【发布时间】: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 线程。那你是怎么做到的呢?
【问题讨论】: