【发布时间】:2014-11-15 19:08:48
【问题描述】:
我找不到更好的方式来表达问题标题中的问题,但希望你能理解......
我有以下代码...
[...]
final JDialog waitingDialog = optionPane.createDialog(optionPane, "Processing in backgroung");
waitingDialog.setLocationRelativeTo(homeFrame);
waitingDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
SwingWorker<Void, Void> backgroundThread = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
// Background processing is done here
System.out.println("[DEBUG] -- Background processing has started");
MyCustomClass.initParameters();
System.out.println("DEBUG] -- initParameters() has finished. Starting main processing now...");
waitingDialog.setVisible(true);
MyCustomClass.run();
return null;
}
// This is called when background thread above has completed
@Override
protected void done() {
waitingDialog.dispose();
};
};
backgroundThread.execute();
[and does other things after this...]
...它执行得很好,直到它到达MyCustomClass.run()。当它应该启动这种方法时,它根本不这样做。应用程序不会暂停、停止或崩溃......它只是在后台处理完成后继续等待继续,但由于某种未知原因而没有发生。
MyCustomClass.run() 是一个static 方法,位于MyCustomClass。我怀疑问题可能出在它的名字上——运行——所以我把它改成了一些随机的东西,比如“doIt()”,但问题仍然存在。
该方法的第一行是一个简单的System.out.println("I got here"),但甚至没有打印出来。
我在方法调用之前和之后添加了断点并尝试调试,但这也无济于事。一切似乎都很好,但我找不到问题所在。
编辑
我问:如何使对话框仅在我希望显示时才显示?换句话说,脚本是:
- SwingWorker 启动时显示“等待”对话框
- 当 initParameter() 发出需要用户输入的选项对话框时隐藏/停止/禁用/处置对话框(按下按钮);
- 在处理恢复时再次显示等待对话框
- 由于后台处理已完成,请丢弃对话框
【问题讨论】:
标签: java multithreading swing debugging swingworker