开发者

How to close JFrame followed by another JFrame?

开发者 https://www.devze.com 2023-01-29 14:23 出处:网络
I have a JFrame panel. It opens several times. However, I want the previous one to close, right before the new one opens.

I have a JFrame panel. 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."

It is the same JFrame but the 开发者_开发问答information (JTextFields) change.

Right now through out the program, where ever there is a method, object calling for the JFrame:

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

and if I have this statement again, it will open another JFrame on top of it. 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. Your code might look something like:

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

openFrame = this;

statusbar sb = new statusbar();

Also, use proper class naming conventions. Words of class names should be capitalized (ie. StatusBar).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号