我有一个JFrame
,这个JFrame
里面有一个JButton
。
我想要在JButton
中先显示JDialog
(显示“请等待”),然后再执行其他代码,最后关闭JDialog
。
但是当显示JDialog
时,停止执行JButton
上的其他代码。
在一个 Thread
上启动其他处理(例如在 SwingWorker
中),并在开始时调用 modalDialog.setVisible(true)
。在任务结束时调用 setVisible(false)
。
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
yordialog.setModal(false)
。
javax.swing
创建或绘制框架需要很长时间。因此,如果 JVM 不等待 swing 完成其工作,则您的消息可能不可见。线程可能会继续执行//在此处执行某些操作
。 - Roshana Pitigala