Java:JDialog 关闭问题
Java: JDialog Closing Issue
在这个程序中,当我关闭 JDialog
对话框时,我遇到了两个问题,对话框没有像 EXIT_ON_CLOSE
那样正确关闭。以及如何给这个对话框赋予标题。
代码
public class Dialog extends JDialog{
public Dialog(){
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
}
}
主要方法
public class 主 {
public static void main(String[] args) {
Dialog frame = new Dialog();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit the application? ",
"EXIT Application", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
System.exit(0);
else if (result == JOptionPane.NO_OPTION) {
frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
}
}
});
}
}
not like EXIT_ON_CLOSE
JDialog 不支持 EXIT_ON_CLOSE
。
but DISPOSE_ON_CLOSE close the dialog slowly
它会立即关闭对话框,焦点将返回到父 JFrame。
Is there a way that i can Close the whole program in JDialog
您需要关闭相框。
也许您正在尝试通过弹出对话框关闭应用程序?如果是这样,请查看 Closing an Application.
它将向您展示如何:
- 使用 WindowListener 处理
windowClosing
并显示弹出对话框,或者
- 使用建议的 class 使编码更容易。
在这个程序中,当我关闭 JDialog
对话框时,我遇到了两个问题,对话框没有像 EXIT_ON_CLOSE
那样正确关闭。以及如何给这个对话框赋予标题。
代码
public class Dialog extends JDialog{
public Dialog(){
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
}
}
主要方法
public class 主 {
public static void main(String[] args) {
Dialog frame = new Dialog();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit the application? ",
"EXIT Application", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
System.exit(0);
else if (result == JOptionPane.NO_OPTION) {
frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
}
}
});
}
}
JDialog 不支持not like EXIT_ON_CLOSE
EXIT_ON_CLOSE
。
but DISPOSE_ON_CLOSE close the dialog slowly
它会立即关闭对话框,焦点将返回到父 JFrame。
Is there a way that i can Close the whole program in JDialog
您需要关闭相框。
也许您正在尝试通过弹出对话框关闭应用程序?如果是这样,请查看 Closing an Application.
它将向您展示如何:
- 使用 WindowListener 处理
windowClosing
并显示弹出对话框,或者 - 使用建议的 class 使编码更容易。