Close window - but don't stop program - JAVA

纵饮孤独 提交于 2021-02-05 08:09:15

问题


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:


  1. You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE

  2. 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?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!