当我按下windows+d时,Swing JDialog将挂起



有一个名为a的窗口,当我单击一个按钮时,对话框B将弹出,然后我单击对话框B上的一个按钮,B将隐藏,此时将进行计算,窗口a将出现。计算完成后,对话框C将出现。但是,当我在对话框C显示之前按Windows+D显示桌面时,对话框将挂起,我必须通过任务管理器终止进程。这里所有的对话框都是模态的。

您可以在JDialog的构造函数中指定父窗口。此外,您可以将对话框指定为模态对话框,以防止在对话框打开时与父窗口进行任何交互。样本代码:

public static void main(String[] args) {
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    JDialog dialog = new JDialog(frame);
    dialog.setSize(200,200);
    dialog.setLocationRelativeTo(frame);
    dialog.setModal(true);  // Works also with .setModal(false)
    dialog.setVisible(true);
}

最新更新