应用程序在 netbeans 7.0 中的 java swing 中关闭 Jframe 时退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5993244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:52:06  来源:igfitidea点击:

Application exits on closing of Jframe in java swing in netbeans 7.0

javaswing

提问by zoheb

i am actually developing GUI application whwn i run this application a login window comes i have a button on it when after successful login i have MDI application form i have menu in that in menu i have menu items when i click menu items Jframe opens upto here i am haing no problem

我实际上是在开发 GUI 应用程序,当我运行这个应用程序时,会出现一个登录窗口 我有一个按钮,成功登录后我有 MDI 应用程序表单 我在菜单中有菜单 我在单击菜单项时有菜单项 Jframe 在这里打开我没问题

but when i close Jframe form entire application exits

但是当我关闭 Jframe 表单时,整个应用程序退出

LOGIN FORM Code

登录表格代码



public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });

BUTTON CODE

按钮代码

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
         NewMDIApplication n = new NewMDIApplication();
         n.show();
         this.setVisible(false);


    }  


MDIApplication form code

MDI申请表代码

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        IVITEM i = new IVITEM();---- this is that form that opens inside the MDI appliaction
        i.show();
    }      


回答by MOHAMED FATHEI

in the IVITEM class add this code

在 IVITEM 类中添加此代码

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

and in the main menu in main window add menu item "exit" and in it's action use

并在主窗口的主菜单中添加菜单项“退出”并在其操作中使用

System.exit(0);

this will close all child windows

这将关闭所有子窗口

回答by Hovercraft Full Of Eels

Your login form is behaving as a dialog and so shouldn't be a JFrame at all but rather a dialog window such as a JDialog, and what's more, should be one that is modalto the main application window (which is likely a JFrame). Many don't realize (and I don't know if this includes you or not) that JDialogs can hold complex GUIs, as complex as any held by a JFrame.

您的登录表单表现为一个对话框,因此根本不应该是 JFrame,而是一个对话框窗口,例如 JDialog,而且,应该是主应用程序窗口(可能是 JFrame)的模态窗口. 许多人没有意识到(我不知道这是否包括您)JDialogs 可以保存复杂的 GUI,就像 JFrame 所保存的一样复杂。

回答by Dapeng

in the JFrame properties panel, set the defaultCloseOperation to DISPOSE

在 JFrame 属性面板中,将 defaultCloseOperation 设置为 DISPOSE