Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 要在用户使用按钮之前停止代码吗_Java_User Interface_Jbutton - Fatal编程技术网

Java 要在用户使用按钮之前停止代码吗

Java 要在用户使用按钮之前停止代码吗,java,user-interface,jbutton,Java,User Interface,Jbutton,所以我尝试运行一个代码,打开一个GUI窗口,在两个按钮之间选择,这两个按钮设置一个值,然后用这个值继续代码的其余部分 @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == testStringA) { setVariableTo = "testString_a"; try { runMethodW

所以我尝试运行一个代码,打开一个GUI窗口,在两个按钮之间选择,这两个按钮设置一个值,然后用这个值继续代码的其余部分

@Override
     public void actionPerformed(ActionEvent e) {
        if(e.getSource() == testStringA) {
            setVariableTo = "testString_a";
            try {
                runMethodWithNewVariable(setVariableTo);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.exit(0);
        } else {
            setVariableTo = "project";
            try {
                runMethodWithNewVariable(setVariableTo);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.exit(0);
        }
     }
我见过类似的问题或教程,但我没有找到适合我的问题的解决方案

正如我已经看到的,JFrameActionListenerActionEvent必须被用来制作带有按钮的GUI

扩展JFrame并实现ActionListener的对象在main方法中编写

问题是,在main方法中编写的代码会打开GUI窗口并继续运行。我只希望代码等待用户单击按钮,然后继续

子解决方案是,在actionPerformed方法中编写我想要的代码,但是:

  • 选择按钮后,GUI窗口保持打开状态
  • actionPerformed方法中编写其余代码对我来说毫无意义
或者编写while循环,直到单击按钮。必须有一个更合理的解决办法,但我不知道或者我不理解这种办法应该如何运作

这是代码的一部分

@Override
     public void actionPerformed(ActionEvent e) {
        if(e.getSource() == testStringA) {
            setVariableTo = "testString_a";
            try {
                runMethodWithNewVariable(setVariableTo);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.exit(0);
        } else {
            setVariableTo = "project";
            try {
                runMethodWithNewVariable(setVariableTo);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.exit(0);
        }
     }

基本上有两个线程在运行——主线程和GUI线程。您没有显式地创建GUI线程,但它就在那里


您可以使用多种技术来同步这两个线程。最基本的是良好的旧
同步
等待
通知
。也可以使用信号量
的东西。在主线程中,您将创建GUI并等待条件满足。在GUI线程(即actionPerformed)中,您将通知。

您基本上有两个线程在运行-主线程和GUI线程。您没有显式地创建GUI线程,但它就在那里


您可以使用多种技术来同步这两个线程。最基本的是良好的旧
同步
等待
通知
。也可以使用信号量
的东西。在主线程中,您将创建GUI并等待条件满足。在GUI线程(即actionPerformed)中,您将通知。

例如,为什么不使用带有两个按钮的JOptionPane(showOptionDialog)来代替JFrame,使用“字符串a”和“项目”来代替“是”和“否”


“显示选项对话框”等JoptionPane本质上是阻塞的。如果在main()方法中放入一个,执行将“等待”用户在对话框中选择某个内容,对话框将返回一个指示器,指示在main()继续执行之前选择的内容。

为什么不使用带有两个按钮的JOptionPane(showOptionDialog),即“字符串a”和“项目”,而不是“是”和“是”“不”,例如


像“show Option Dialog”这样的joption窗格本质上是阻塞的。如果在main()方法中放入一个,执行将“等待”用户在对话框中选择某个内容,对话框将返回一个指示符,指示在main()中执行之前选择的内容继续。

在程序开始时,向用户显示模式
JDialog
!您可以使用
JOptionPane.show()
方法执行此操作,如下所示:

String[] buttonTexts = {"first","second"}; //create the button texts here

//display a modal dialog with your buttons (stops program until user selects a button)
int userDecision =  JOptionPane.showOptionDialog(null,"title","Select a button!",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,buttonTexts,buttonTexts[0]);

//check what button the user selected: stored in the userDecision
// if its the first (left to right) its 0, if its the second then the value is 1 and so on

if(userDecision == 0){
  //first button was clicked, do something
} else if(userDecision == 1) {
  //second button was clicked, do something
} else {
 //user canceled the dialog
}

//display your main JFrame now, according to user input! 

在程序开始时,向用户显示模式
JDialog
!可以使用
JOptionPane.show()
方法执行此操作,如下所示:

String[] buttonTexts = {"first","second"}; //create the button texts here

//display a modal dialog with your buttons (stops program until user selects a button)
int userDecision =  JOptionPane.showOptionDialog(null,"title","Select a button!",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,buttonTexts,buttonTexts[0]);

//check what button the user selected: stored in the userDecision
// if its the first (left to right) its 0, if its the second then the value is 1 and so on

if(userDecision == 0){
  //first button was clicked, do something
} else if(userDecision == 1) {
  //second button was clicked, do something
} else {
 //user canceled the dialog
}

//display your main JFrame now, according to user input! 

只是想澄清一下:在JFrame上调用
setVisible()
后,是否希望您的
main()
方法被阻止?是的,完全正确。我在主方法setVisible(true)中初始化GUI对象接下来是另一个使用用户输入的方法。这很有意义,代码只会运行,不会因用户输入而停止。只是想澄清一下:在JFrame上调用
setVisible()
方法后,是否希望
main()
方法被阻止?是的,完全正确。我在main方法setVisible中初始化GUI对象(true)和之后的另一个使用用户输入的方法。这很有意义,代码只会运行,不会因为用户输入而停止。Jap!这就是我要找的:)作为旁注。在(win10)中的弹出窗口不会触发与JFrame(橙色“flashin”灯)类似的通知在任务栏中,很难看到多个程序何时打开。有选项吗?找到这个,可能就是你要找的。Jap!这就是我要找的:)作为旁注。在(win10)中的弹出窗口不会触发类似于JFrame(橙色“闪光”灯)的通知在任务栏中,使您很难看到多个程序何时打开。是否有此选项?找到此,可能就是您正在寻找的。