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
               }
           }});