简体   繁体   English

如何关闭JFrame之后再关闭另一个JFrame?

[英]How to close JFrame followed by another JFrame?

I have a JFrame panel. 我有一个JFrame面板。 It opens several times. 它打开了几次。 However, I want the previous one to close, right before the new one opens. 但是,我希望在新版本打开之前关闭前一个。

So is there a way to add some code implying "Close this JFrame when there is a trigger to execute the same JFrame." 因此,有一种方法可以添加一些代码,暗示“当有触发器执行同一JFrame时,请关闭此JFrame”。

It is the same JFrame but the information (JTextFields) change. 它是相同的JFrame,但信息(JTextFields)有所变化。

Right now through out the program, where ever there is a method, object calling for the JFrame: 现在,在整个程序中,只要有方法,对象就会调用JFrame:

statusbar sb = new statusbar();
        sb.setting();

and if I have this statement again, it will open another JFrame on top of it. 如果我再次有此语句,它将在其顶部打开另一个JFrame。 but i want to close the previous one. 但我想关闭上一个。

Keep a static variable in your class for the current frame that is displayed. 在类中为显示的当前帧保留一个静态变量。 Then in the constructor of your class you check this variable. 然后在类的构造函数中检查此变量。 If it is not null then you invoke dispose() on the variable to close the frame. 如果它不为null,则在变量上调用dispose()以关闭框架。 Your code might look something like: 您的代码可能类似于:

private static Jframe openFrame;
...
...
if (openFrame != null)
{
    openFrame.dispose();
}

openFrame = this;

statusbar sb = new statusbar(); statusbar sb =新的statusbar();

Also, use proper class naming conventions. 另外,使用正确的类命名约定。 Words of class names should be capitalized (ie. StatusBar). 类名的单词应大写(即StatusBar)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM