3

プロジェクトで誤って閉じるのを止めたいです。JFrameフォームをホームページとして使用しています。ホームウィンドウの閉じるボタンをクリックすると、はいオプションに終了コードを入れます。[オプションなし] をクリックしたときに閉じるのをやめたいです。方法はありますか。これが私のコードです。私はネットビーンズ7.3を使用しています

 private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
 int i= JOptionPane.showConfirmDialog(null, "Are you sure to exit?");
    if (i == JOptionPane.YES_OPTION) {
        System.exit(0);
    } else{
        new Home().setVisible(true);

    }
}
4

2 に答える 2

6

どうですか

class MyGUI extends JFrame {

    public MyGUI() {
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// <- don't close window 
                                                             // when [X] is pressed

        final MyGUI gui = this;
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int i = JOptionPane.showConfirmDialog(gui,
                        "Are you sure to exit?", "Closing dialog",
                        JOptionPane.YES_NO_OPTION);
                if (i == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });

        setSize(200, 200);
        setVisible(true);
    }

    //demo
    public static void main(String[] args) {
        new MyGUI();
    }
}
于 2013-10-10T16:44:57.320 に答える