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を押しても終了します。のクローズ操作を手動で処理する場合と同様に、代わりにデフォルトのクローズ操作を設定する必要があります。OKCANCELDO_NOTHING_ON_CLOSEJFrame

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 に答える