简体   繁体   中英

how do i close a frame yet open a new frame?

how do i close a frame yet open a new frame?

i have a frame, (help)

when i click on my menu item i want to open (mainForm) exit from help.

new mainForm().setVisible(true); System.exit(0);

i know this closes the whole program however how do i get it to only close the current frame

thanks

If you no longer want to use the frame you could use frame.dispose()

If you just want to hide it use frame.setVisible(false) .

If you extended a Frame and are trying to close it from within use this.dispose or this.setVisible(false) .

You should rethink your requirments. For the user, it would be best to have both the program and the help window visible at the same time. Closing the main window when showing the help screen and vice versa is really, really bad for usability - you shouldn't do it. We've had window-based GUIs for almost 30 years now - showing several windows on screen at the same time is what they're for!

I think you should hide the frame you do not wish shown with setVisible(false). System.exit(0) stops the JVM ending the entire program.

In short, the semantics of setVisible has nothing to do with starting / stopping the application.

If you want to start an entire new application you'd have to look at Runtime.exec(). I don't really know if you can close the parent process (the Help application) with exit() though.

Let's say you created your frame as so:

 JFrame mainframe = new JFrame("Radio Notes");
//show Frame
mainframe.setVisible(true);
//close the frame  
mainframe.dispose();

try setting the default close operation for the JFrame like so.

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

Then implement a WindowListener that performs the actions you want when a window closing event is fired.

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