java 从一个 JFrame 移动到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11281185/
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-31 04:30:52  来源:igfitidea点击:

Moving from one JFrame to another

javaswingnetbeansjframe

提问by Ali Bassam

In a package in Netbeans I created two JFrame Forms, first one is Login, second is, mainProgram, after the successful log in, I use the following way to "close" the Login frame and open the main program frame.

在Netbeans的一个包中我创建了两个JFrame Form,第一个是Login,第二个是mainProgram,登录成功后,我用下面的方式“关闭”了Login框架,打开了主程序框架。

mainProgram m=new mainProgram();
m.setVisible(true);
setVisible(false); //to hide the log in frame

Is this the correct way? Isn't it wrong if these two separated classes are hidden instead of being closed? are these one process or two different processes? if there's a better way then what is it?

这是正确的方法吗?如果这两个分开的类被隐藏而不是被关闭是不是错了?这是一个过程还是两个不同的过程?如果有更好的方法,那是什么?

thanks..

谢谢..

采纳答案by GETah

Is this the correct way?

这是正确的方法吗?

Yes, this should be fine.

是的,这应该没问题。

isn't it wrong if these 2 separated classes are hidden instead of being closed?

如果这两个分开的类被隐藏而不是被关闭,这不是错了吗?

The ideal is dispose of your unused forms (such as the login form when not needed any more)

理想的是处理您未使用的表单(例如不再需要时的登录表单)

are these 1 process or 2 different processes?

这些是 1 个过程还是 2 个不同的过程?

These will run on the same process

这些将在同一进程上运行

回答by mKorbel

In a package in Netbeans I created 2 JFrame Forms, first one is Login, second is, mainProgram, after the successful log in, I use the following way to "close" the Login frame and open the main program frame.

在Netbeans的一个包中我创建了2个JFrame Forms,第一个是Login,第二个是mainProgram,登录成功后,我用下面的方式“关闭”Login框架,打开主程序框架。

use CardLayout, after correct login you can to switch the GUI to next Card and/or with change for JFrame Dimmnsion on the screen too,

使用CardLayout,正确登录后,您可以将 GUI 切换到下一张卡片和/或更改屏幕上的 JFrame Dimmnsion,

回答by Pavel K.

in my opinion the more correct way is to use another class, like Launcher, which will have the entry point (main method). Make the login window as a modal JDialog, and set DISPOSE_ON_CLOSE as a value of default close operation. The class of dialog should contain a method to inform a user really logged in. After the login dialog is closed, show the main frame

在我看来,更正确的方法是使用另一个类,例如 Launcher,它将具有入口点(main 方法)。将登录窗口设为模态 JDialog,并将 DISPOSE_ON_CLOSE 设置为默认关闭操作的值。对话框类应该包含一个方法来通知用户真正登录。 登录对话框关闭后,显示主框架

loginDialog.setVisible(true);
if (loginDialog.isLoggedIn())
    mainFrame.setVisible(true);

回答by Kumar Vivek Mitra

Try this...

试试这个...

  1. The approach you used to hide and un-hide is fine, but will be better if dispose is used.

  2. Try applying the Singleton patternon the classes which govern these JFrames.

  3. And yes they both will be on the same Process.

  1. 你用来隐藏和取消隐藏的方法很好,但如果使用 dispose更好

  2. 尝试在管理这些 JFrame 的类上应用单例模式

  3. 是的,他们都将在同一个 Process 上