如何停止关闭JFrame表单?

时间:2013-10-10 16:40:27

标签: 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);

    }
}

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:0)

你可以像这样做

(0, 5)
(1, 6)
(2, 7)
(3, 8)
(4, 9)
(5, 10)
(6, 11)
(7, 12)
(8, 13)
(9, 14)
(10, 15)