简体   繁体   中英

How to close JFrame followed by another JFrame?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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