Java 为什么Frme windowClosing with ConfirmDialog同时关闭这两种情况(确定和取消)?
这是我的密码:Java 为什么Frme windowClosing with ConfirmDialog同时关闭这两种情况(确定和取消)?,java,swing,jframe,windowlistener,Java,Swing,Jframe,Windowlistener,这是我的密码: addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int a = JOptionPane.showConfirmDialog(null, "Are you sure you want t
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\u选项
或a==CANCEL\u选项
帧将关闭
为什么?您可能已经将
JFrame
的默认关闭操作设置为EXIT\u ON\u close
。因此,无论按OK
或CANCEL
,都会退出JFrame
。如果要手动处理JFrame
的关闭操作,则应将默认关闭操作设置为DO\u NOTHING\u ON\u close
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
}
}});