Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 等待按钮按下(在GUI中)在代码中前进_Java_Swing_User Interface_Thread Safety - Fatal编程技术网

Java 等待按钮按下(在GUI中)在代码中前进

Java 等待按钮按下(在GUI中)在代码中前进,java,swing,user-interface,thread-safety,Java,Swing,User Interface,Thread Safety,多亏了这里有帮助的回复,我已经得到了我想要的GUI。 --> 我目前面临的问题是,如何“中断”代码的执行并让代码等待 这是GUI,这是添加了ActionListener的当前代码: import java.awt.*; import javax.swing.*; public class GUITest extends JFrame { JButton testButton1 = new JButton("Do something"); JButton testButton2 = n

多亏了这里有帮助的回复,我已经得到了我想要的GUI。 -->

我目前面临的问题是,如何“中断”代码的执行并让代码等待

这是GUI,这是添加了ActionListener的当前代码:

import java.awt.*;  
import javax.swing.*;  

public class GUITest extends JFrame {

JButton testButton1 = new JButton("Do something");
JButton testButton2 = new JButton("Do something different");
JButton testButton3 = new JButton("Do something even more different");

 public GUITest() {

      super("Testing Title");  
      Container pane = getContentPane();  
      pane.setLayout(new BorderLayout());
      pane.add(getHeader(),BorderLayout.NORTH); 
      pane.add(getTextArea(),BorderLayout.CENTER);
      pane.add(getButtonPanel(),BorderLayout.SOUTH);

}  

 public JComponent getHeader() {  

     JPanel labelPanel = new JPanel();  
     labelPanel.setLayout(new GridLayout(1,2));  
     labelPanel.setSize(getPreferredSize());    
     JLabel labelLocal = new JLabel("Left value: ", JLabel.CENTER);  
     JLabel labelDB = new JLabel("Right value: ", JLabel.CENTER);  
     labelPanel.add(labelLocal);
     labelPanel.add(labelDB);
     return labelPanel;

  }

public JComponent getTextArea() {  

   JPanel textPanel = new JPanel();    
   textPanel.setLayout(new GridLayout(1,2,5,0));

   JTextArea testTextArea = new JTextArea();
   testTextArea.setEditable(false);
   JScrollPane sp1 = new JScrollPane(testTextArea); 

   JTextArea testTextArea2 = new JTextArea();
   JScrollPane sp2 = new JScrollPane(testTextArea2); 
   testTextArea2.setEditable(false);

   testTextArea.setText("Hello Hello Hello\nTesting!\ntesterino\ntesteroni");
   testTextArea2.setText("Hello Hello Hello\nTesting!\ntest\nABC123\ncdef123\nhijk123");

   textPanel.add(sp1);
   textPanel.add(sp2);
   return textPanel;
}

 public JComponent getButtonPanel() {

  JPanel buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout());

  testButton1.addActionListener(this); 
  testButton2.addActionListener(this); 
  testButton3.addActionListener(this); 

  buttonPanel.add(testButton1);  
  buttonPanel.add(testButton2);  
  buttonPanel.add(testButton3);  
  return buttonPanel;   
}

   public void actionPerformed(ActionEvent event) {
if(event.getSource() == this.testButton1){
    System.out.println("testButton1!");
}

if(event.getSource() == this.testButton2){
    System.out.println("testButton2!");
}

if(event.getSource() == this.testButton3){
    System.out.println("testButton3!");
}
}  

public static void main(String[] args) {  

  GUITest e = new GUITest();  
  e.setVisible(true);  
  e.setMinimumSize(new Dimension(650, 200));
  e.pack();  
  e.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  e.setLocationRelativeTo(null);  
  }  
}  
我读过一些关于使用JOptionPane的文章,但显然这并没有给我想要的灵活性,因为我不仅需要按钮,还需要两个标签和文本区域

基本上,我想做的是:

//...
//other code that comes before

GUITest.openGUI();
if(button1 == pressed)
{
  //do something and hide GUI
}
else if(button2 == pressed)
{
  //do something else and hide GUI
}
//afterwards, there is more code, but it only gets executed after a button press
//other code that comes before
GUITest.openGUI();
if(button1 == pressed)
{
  //do something and hide GUI
}
else if(button2 == pressed)
{
  //do something else and hide GUI
}
//afterwards, there is more code, but it only gets executed after a button press
我希望执行停止,直到我选择按下按钮1或按钮2为止。我不想做wait(),因为这可能会导致性能下降

谢谢你的帮助

我读过一些关于使用JOptionPane的文章,但显然这并没有给我想要的灵活性,因为我不仅需要按钮,还需要两个标签和文本区域

然后,您将需要阅读更多关于JOptionPane的内容,因为它肯定可以显示所有这些动物以及更多。请理解
JOptionPane.showXxx(…)
方法的对象参数可以将任何Swing组件作为其值。这意味着JOptionPane可以接受并显示一个JPanel,其中包含多个组件,包括其他JPanel,实际上可以包含一个完整的复杂GUI

另一个选项是创建自己的JDialog,使其成为模态,并在其中显示停止的GUI;两种方法都可以


基本上,我想做的是:

//...
//other code that comes before

GUITest.openGUI();
if(button1 == pressed)
{
  //do something and hide GUI
}
else if(button2 == pressed)
{
  //do something else and hide GUI
}
//afterwards, there is more code, but it only gets executed after a button press
//other code that comes before
GUITest.openGUI();
if(button1 == pressed)
{
  //do something and hide GUI
}
else if(button2 == pressed)
{
  //do something else and hide GUI
}
//afterwards, there is more code, but it only gets executed after a button press

在这里,您可能误解了事件驱动程序的工作方式。这里不需要“等待”任何事情,而是当事件发生时,您会对事件做出反应,例如按钮按下。关键是让听众对按下按钮做出反应,并根据节目的状态改变这种反应。

你必须改变你的思维方式。GUI应用程序是事件驱动的。也就是说:原则上,它一直在“等待”。按下按钮将引发一个事件,您可以为其注册处理程序。该处理程序的代码将在相应事件发生后立即执行。感谢您的回复。您说JOptionPane可以接受任何JPanel。那么是否有可能将我已经拥有的GUI放入JOptionPane?如果是,我用哪一个?showOptionDialog?另外,供您编辑:我可能用词不正确,但肯定需要“等待”。基本上,我希望程序(做其他事情)显示一个小的GUI/对话框,让用户选择他想做的事情,并根据这一点,程序进一步进行。它等待用户输入。JDialog等待用户输入,这正是我想要的。如何将上述代码“转换”为外观完全相同的JDialog?谢谢。@OldMcDonald:我避免创建扩展顶级窗口(如JFrame或JDialog)的类,因为这样会把你拖进一个角落。相反,让类扩展或创建一个JPanel,从中获取所有JFrame代码,然后将其放入JOptionPane,或者动态创建一个JDialog并将其放入其中。