简体   繁体   中英

JFrame wont dispose on button action performed

I'm trying to do an app that switches between 2 windows.

The main window is only used once at the start of the app so the user can input some data.

All the work is done between the other 2 frames. Only one frame must be available or visible at a time.

My problem is that I cant dispose this 2 windows, so every time I switch a new instance of the window is created and running in the background.

I've tried to destroy this windows but it seems that one window cannot destroy itself.

I expect to create and destroy windows, not just change its visibility.

"My problem is that I cant dispose this 2 windows, so every time I switch a new instance of the window is created and running in the background." You need to create your new JFrame object, and then dispose of your old window by performing the dispose() method on your old window which you're wanting to get rid of. Something like this:

//When switch event happens(like a button press)
JFrame newWindow = new JFrame()
this.dispose() //Assuming this class is your JFrame subclass

If your JFrame just holds a variable value do it like this:

JFrame newWindow = new JFrame()
    jframeName.dispose() 

You need to provide code so we can help you better.

Edit: how are you even changing which window runs? Are you using buttons on each one? Please provide context and clarity

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