如何回到 Java 中的 JFrame
我在 JFrame 中有一个按钮,如果按下它,它会将我们带到另一个框架.我使用了这段代码:
I have a button in a JFrame, if pressed, it takes us to another frame. I used this code:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
SecondForm secondform = new SecondForm();
secondform.setVisible(true);
setVisible(false);
dispose();
}
所以新框架打开并且一切正常.然后我在第二帧中放置了另一个按钮,以便返回上一帧.我使用了这段代码:
So the new frame opens and everything is ok. Then i placed another button -in the second frame- in order to go back to the previous frame. I used this code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
MainForm Mform = new MainForm();
Mform.setVisible(true);
setVisible(false);
dispose();
}
问题是,我认为这不是正确的做法.我想要的是:
The thing is, i don't think this is the right way to do this. What i want is to:
- 隐藏第一帧
- 显示新的第二个
- 处理第二个
- 再次显示第一个
有没有办法使用第一个 MainForm 实例而不是每次我想返回时都创建一个新实例.我监控了我的程序,每次我来回切换帧时,正如我所怀疑的那样,它使用的内存不断增加.
Is there a way to do that using the first MainForm instance and not creating a new one every time i want to go back. I monitored my program and every time i go back and forth the frames and as i suspected, the ram being used by it keeps increasing.
提前致谢.
我有一个登录系统,当用户输入正确的凭据时,就会创建一个新的 ManiForm 实例.
EDIT : I have a login system and when the user put the correct credentials a new ManiForm instance is created.
MainForm Mform = new MainForm();
Mform.setVisible(true);
这就是我要使用的实例.ii 有没有办法让 MForm 从 secondform 再次可见?
That is the instance i want to use. Ii there a way to make MForm visible again from the secondform?
首先感谢您的帮助!
我同意不使用多个 JFrame 会更容易,但你能告诉我哪种方法更好地完成我在第一篇文章中提出的要求吗?Robin 给我的答案非常好,但我不知道该放什么作为论据*:
I agree that it is easier not to use more than one JFrames, but can you please tell me which is the better way to do what i asked in the first post? The answer Robin gave me is very nice but i don't know what to put as an argument there*:
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
* new SecondForm().setVisible(true);
}
});
它来自 NetBeans 自动生成的代码.
It's from the auto-generated code from NetBeans.
我试过了
new SecondForm(super).setVisible(true);
但我仍然得到编译错误.显然我必须放 super.something()
但我不知道是什么.我尝试了很多,但没有成功.
but i still get compile errors. Apparently i must put super.something()
but i don't know what. I tried many but no luck.
推荐答案
你不应该使用超过一帧.
you shouldn't use more then one frame.
除了 defaultExitOperation、size、preferedsize 和 visible true 之外,您应该在 JFrame 中没有任何内容.而是将所有按钮和字段放入 JPanel 中,然后在 JFrame 中添加/删除 JPanel.
You should have NOTHING in JFrame except defaultExitOperation, size, preferedsize and visible true. Instead place all buttons and fields into a JPanel and add/remove the JPanel from the JFrame.
如果您想打开另一个窗口,请使用 JDialog.
If you want another Window open use JDialog.
顺便说一句:您可以将 MainFrame 设置为 false 并以 MainFrame 作为父级打开一个 JDialog.只有当有人写下正确的用户 + 密码时,您才能使 MainFrame 可见.
btw: you can have your MainFrame setVisible false and open a JDialog with your MainFrame as parent. Only if someone writes down the right user + password you make the MainFrame visible.
相关文章