关闭 JFrame => 程序崩溃

Closing JFrame => Program crashes

我有两个JFrame

我正在通过单击 ApplicationsJFrame 中的 Button 来设置 InsertApplicationsForm 可见。

每次我关闭 InsertApplicationsFormApplicationsJFrame 也会关闭,但程序仍然是 运行,当我最小化 InsertApplicationsFormApplicationsJFrame 最小化,但不会再最大化...

ApplicationsJFrame:

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("T-AMS");
setFocusableWindowState(false);

InsertApplicationsForm:

public class InsertApplicationForm extends javax.swing.JFrame {

    /**
     * Creates new form InsertApplicationForm
     */
    public InsertApplicationForm() {
        initComponents();
    }

[...]

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Insert Application")
addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

private void formWindowClosed(java.awt.event.WindowEvent evt) {                                  
        ApplicationsJFrame.insert = false;
    }

打开InsertApplicationsForm:

new InsertApplicationForm().setVisible(true);

您应该只将 EXIT_ON_CLOSE 常量 int 添加到主 JFrame。 相反,将 WindowListener 添加到您的临时框架中:

//Suppose you have a custom Frame in your Form class
class InsertApplicationsForm extends JFrame{
//Some stuff
//...

public InsertApplicationsForm(){
//Implement
    //DO NOT ADD setDefaultCloseOperation
setVisible(true);
}

addWindowListener(new WindowAdapter(){

     @Override
    public void windowClosing(WindowEvent e){
        InsertApplicationsForm.this.dispose();
    }

});

}

因此,在您的主要 class 或方法、事件中,随便调用一下:

new InsertApplicationsForm();

并且会自动打开一个新的JFrame,您可以关闭它而不影响主框架或其他框架。您甚至可以实例化多个 class 框架并且关闭不会影响其他框架。