【问题标题】:Want to halt code until user uses a button想要在用户使用按钮之前暂停代码
【发布时间】:2018-11-28 04:58:17
【问题描述】:

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

我看过类似的问题或教程,但我没有找到适合我的问题的解决方案。

正如我已经看到的,必须使用 JFrame、ActionListener 和 ActionEvent 来制作带有按钮的 GUI。

在main方法中写了一个扩展JFrame并实现ActionListener的Object。

问题是,在 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);
        }
     }

【问题讨论】:

  • 澄清一下:您是否希望在您的 JFrame 上调用 setVisible() 后阻止您的 main() 方法?
  • 是的。我在 main 方法 setVisible(true) 中初始化 GUI 对象,然后遵循另一个使用用户输入的方法。这是有道理的,代码只会运行,不会因用户输入而停止。

标签: java user-interface jbutton


【解决方案1】:

为什么不使用 JOptionPane (showOptionDialog) 来代替 JFrame,例如使用“string A”和“project”而不是“Yes”和“No”两个按钮?

像“显示选项对话框”这样的 JOptionPanes 本质上是阻塞的。如果您在 main() 方法中放置一个,执行将“等待”用户在对话框中选择某些内容,并且对话框将返回一个指示符,指示在 main() 继续执行之前选择的内容。

【讨论】:

    【解决方案2】:

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

    您可以使用多种技术来同步这两个线程。最基本的就是好老的synchronized、wait和notify。也可以使用Semaphore。在主线程中,您将创建 GUI 并等待满足条件。在 GUI 线程(即 actionPerformed)中,您会通知。

    【讨论】:

      【解决方案3】:

      在程序开始时,向用户显示模态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! 
      

      【讨论】:

      • 日本!这就是我一直在寻找的 :) 作为旁注。 (win10)中的弹出窗口不会触发类似于任务栏中带有JFrame(橙色“闪烁”灯)的通知,因此很难看到何时打开了多个程序。有这样的选择吗?
      • kodejava.org/how-do-i-flash-the-window-taskbar-in-swing 找到这个,可能就是你要找的。​​span>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      相关资源
      最近更新 更多