如何回到Java中的JFrames [英] How to go back to a JFrames in Java

查看:99
本文介绍了如何回到Java中的JFrames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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:


  • 隐藏第一帧

  • 显示新的第二帧

  • 处理第二个

  • 再次显示第一个

  • hide the first frame
  • show the new second one
  • dispose the second one
  • show again the first

有没有办法使用第一个MainForm实例,而不是每次想要返回时都创建一个新实例。
我监控我的程序,每次来回移动框架,我怀疑,它使用的ram不断增加。

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);

这是我想要使用的实例。有没有办法让第二种形式的MForm再次可见?

That is the instance i want to use. Ii there a way to make MForm visible again from the secondform?

首先感谢你的帮助!

我同意不使用多个JFrame更容易,但是请您告诉我哪个更好的方式来做我在第一篇文章中提出的问题?
答案罗宾给了我很好的但我不知道该把什么作为参数*:

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中有NOTHING。
而是将所有按钮和字段放入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.

如果您想要打开另一个Window,请使用JDialog。

If you want another Window open use JDialog.

顺便说一下:你可以让你的MainFrame setVisible为false并打开一个JDialog,将你的MainFrame作为父级。只有当有人写下正确的用户+密码时,才能使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.

这篇关于如何回到Java中的JFrames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆