0

这是我的代码:

            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将关闭。

为什么?

4

1 回答 1

2

您可能已将默认关闭操作设置为JFrameas 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
               }
           }});
于 2013-03-30T11:51:47.877 回答