如何停止关闭JFrame表单?

Dil*_*ini 1 java swing jframe netbeans-7 windowlistener

我想在我的项目中不小心关闭.我正在使用JFrame Form作为主页.当我单击主窗口的关闭按钮时,我将退出电话线置于是选项中.我想点击No Option时停止关闭.有什么办法吗?这是我的电话.我正在使用netbeans 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);

    }
}
Run Code Online (Sandbox Code Playgroud)

Psh*_*emo 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();
    }
}
Run Code Online (Sandbox Code Playgroud)