如何停止关闭 JFrame 表单



我想停止意外关闭我的项目。我正在使用JFrame Form作为主页。当我单击主窗口的关闭按钮时,我将退出线放在是选项中。我想在单击"无选项"时停止关闭。有什么办法吗。这是我的绳子。我正在使用 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);
    }
}

怎么样

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();
    }
}
你可以

像这样简单地做到这一点

Integer opt =  JOptionPane.showConfirmDialog(null, "This application cannot continue without login to the databasen Are you sure you need to exit...?", "Application X", JOptionPane.YES_NO_OPTION);
if(opt== JOptionPane.NO_OPTION){
    this.setVisible(true);
}else{
    System.exit(0);
}

最新更新