问题
In my program it opens a window if an action is happened. After I have filled out information in this window, and entered a button, the window dispose().
The window is picked up in a program outside my main program, but when I close this window, my main program stops. How can I prevent this from happening?
Thanks for your help!
回答1:
You can set the
defalaultCloseOperation
property of the second frame toDO_NOTHING_ON_CLOSE
orDISPOSE_ON_CLOSE
Don't even use two frames. Use a modal
JDialog
instead for a secondary frame. See more at How to Use Dialogs. Read more about Modality. And for a good read, see The Use of Multiple JFrames, Good/Bad Practice?Forget about number 1. and go straight to 2.
回答2:
If using JFrame or extending it you can use setDefaultCloseOperation() method like:
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
回答3:
The dispose
command is from the AWT Bundle, and this may cause problems, as you are attempting to "close" a swing window with an AWT command.
You can close the window with this command:
windowName.setVisable(false);
windowName
is the name of the object representing the window. If you are extending a class, and have no object, you can use this
More Information on the Dispose Method: "In general java.awt.Window.dispose() is used on a GUI component that is a subclass of Window, in order for that specific GUI Window object (and its children) to properly release and destroy native UI resources (such as the screen). Garbage collection (and the eventual exiting of the entire VM) may happen as a result of dispose(), but is not directly connected with the dispose() call itself." From: https://www.java.net/node/684412
回答4:
windowName.setVisable(false);
doesn't seems to be a good choice. What if user want to exit the program?
check this question - How to hide a JFrame in system tray of taskbar
来源:https://stackoverflow.com/questions/23739578/close-window-but-dont-stop-program-java