【发布时间】:2012-02-05 06:15:38
【问题描述】:
我有一个JFrame,而这个JFrame 有一个JButton。
我想在JButton中首先显示JDialog(显示"Please wait")并执行其他代码然后关闭JDialog。
但是当显示JDialog 时停止在JButton 上执行其他代码。
【问题讨论】:
我有一个JFrame,而这个JFrame 有一个JButton。
我想在JButton中首先显示JDialog(显示"Please wait")并执行其他代码然后关闭JDialog。
但是当显示JDialog 时停止在JButton 上执行其他代码。
【问题讨论】:
在Thread 上开始其他处理(例如在SwingWorker 中),并在开始时调用modalDialog.setVisible(true)。任务结束时调用setVisible(false)。
【讨论】:
我建议创建一个简单的 JDialog,然后在您的代码运行后处理它。您可以使用以下代码创建您的 JDialog:
JDialog dialog = new JDialog();
JLabel label = new JLabel("Please wait...");
dialog.setLocationRelativeTo(null);
dialog.setTitle("Please Wait...");
dialog.add(label);
dialog.pack();
并像这样实现它:
dialog.setVisible(true); // show the dialog on the screen
// Do something here
dialog.setVisible(false); // set visibility to false when the code has run
【讨论】:
javax.swing 需要花费大量时间来创建或绘制框架。因此,您的消息可能不可见,因为 JVM 不会等到 swing 完成其工作。该线程可能会转到//Do something here。
可能 jdialog 处于模态模式,尝试更改 jdialog 的 modal 属性:yordialog.setModal(false)。
【讨论】:
它停止是因为您正在使用“MAIN-Thread”来执行代码并显示 JDialog。
要解决这个问题,您应该查看类似SwingWorker
【讨论】: