这是我的代码:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int a = JOptionPane.showConfirmDialog(null,
"Are you sure you want to exit the program?", "Exit Program ",
JOptionPane.YES_NO_OPTION);
System.out.println(a);
if(a==JOptionPane.OK_OPTION){
dispose();
}
}});
问题是a==OK_OPTION
或a==CANCEL_OPTION
框架将关闭。
为什么?
答案 0 :(得分:2)
您可能已将JFrame
的默认关闭操作设置为EXIT_ON_CLOSE
。因此,无论您按JFrame
还是OK
,都会退出CANCEL
。如果要手动处理DO_NOTHING_ON_CLOSE
的关闭操作,则应将默认关闭操作设置为JFrame
。
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int a = JOptionPane.showConfirmDialog(null,
"Are you sure you want to exit the program?", "Exit Program ",
JOptionPane.YES_NO_OPTION);
System.out.println(a);
if(a==JOptionPane.OK_OPTION){
dispose();//You can use System.exit(0) if you want to exit the JVM
}
}});