【问题标题】:(Java Swing) Stalling actionPerformed Command Until A Dialog Returns(Java Swing) 停止 actionPerformed 命令直到对话框返回
【发布时间】:2014-04-30 11:34:19
【问题描述】:

我需要创建一个 JDialog 或一个 JFrame 类(只要它是一个图形界面,这并不重要) - 可以在从 actionPerformed(ActionEvent) 处理程序中调用的静态命令中使用并将停止该过程,直到用户从用户界面提供他们的选择。

显然可以这样做,因为这正是 JOptionPane 创建的对话框所做的。但是当我尝试停止线程直到用户完成选择过程时,界面无法正常显示。

这是我遇到问题的代码部分:

public static Results queryForResults(String message, int propertyOne, 
      int propertyTwo){

    MyCustomDialog mcd = new MyCustomDialog(message, propertyOne,propertyTwo);
    mcd.show();
    // complete() is a boolean property of my MyCustomDialog class that turns
    // true when the interface has been completed.
    while(!mcd.complete()){
        synchronized(mcd){
            try{
                mcd.wait(10000L);
            } catch(InterruptedException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    mcd.dispose();
    // My MyCustomDialog class has a getResults() property which returns
    // a Results object (i.e. another custom class I've made to contain
    // the selections made by the user.)
    return mcd.getResults();
}

对话框外框出现,但内部没有出现。它们似乎只是对话框下方屏幕上出现的任何内容的屏幕截图。我的印象是这不起作用,因为我不应该使用 wait 命令来停止 Swing Event 线程。那你是怎么做到的呢?

【问题讨论】:

    标签: java swing events dialog


    【解决方案1】:

    不需要while循环。 modal JDialog 将导致 ActionLIstener 中的执行停止,直到对话框关闭。

    另外,不要使用 show() 方法来显示对话框。您应该使用setVisible(true) 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      相关资源
      最近更新 更多