对Swing中关闭窗口参数的理解
对Swing中关闭窗口参数的理解API对参数的理解HIDE_ON_CLOSEDO_NOTHING_ON_CLOSEDISPOSE_ON_CLOSEEXIT_ON_CLOSESwing中关闭窗口的方法是javax.swing.JFrame.setDefaultCloseOperation(int operation)即:设置JFrame 框架的默认关闭操作APIpublic void set...
对Swing中关闭窗口参数的理解
Swing中关闭窗口的方法是
javax.swing.JFrame.setDefaultCloseOperation(int operation)
即:设置JFrame 框架的默认关闭操作
API
public void setDefaultCloseOperation(int operation)
Sets the operation that will happen by default when the user initiates a “close” on this frame. You must specify one of the following choices:
- DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don’t do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
- HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
- DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
- EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.
The value is set to HIDE_ON_CLOSE by default. Changes to the value of this property cause the firing of a property change event, with property name “defaultCloseOperation”.
Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information.
Parameters:
operation - the operation which should be performed when the user closes the frame
Throws:
- IllegalArgumentException - if defaultCloseOperation value isn’t one of the above valid values
- SecurityException - if EXIT_ON_CLOSE has been specified and the SecurityManager will not allow the caller to invoke System.exit
See Also:
Window.addWindowListener(java.awt.event.WindowListener), getDefaultCloseOperation(), WindowConstants, Runtime.exit(int)
对参数的理解
HIDE_ON_CLOSE
在以上高亮的参数中,默认的是HIDE_ON_CLOSE ,即当用户关闭窗口时,它会隐藏起来,这条线程及其父线程并不会结束,即任务管理器中仍然存在该窗口的进程;
DO_NOTHING_ON_CLOSE
该参数是指不进行任何操作,不关闭也不隐藏,而是会触发窗口监听器的windowClosing 方法,即要求开发人员在该方法中处理用户发起的关闭窗口操作;
DISPOSE_ON_CLOSE
该参数是指隐藏并丢弃该框架,丢弃(dispose)是指释放由此框架、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。即这些组件的资源将被破坏,它们使用的所有内存都将返回到操作系统,并将它们标记为不可显示。
注意:
- 此处释放的是屏幕资源而不是所有资源,这意味着可以使用pack 或 show 方法恢复,并且可以恢复到dispose前的状态。
- 如果丢弃掉最后的窗口(程序启动就显示的那个),程序将终止。即如果你的程序没有其他线程在运行的话,当所有的窗口都被丢弃了之后,JVM将退出。
EXIT_ON_CLOSE
该参数是指使用 System exit 方法退出应用程序,且仅在应用程序中使用。即不仅关闭窗口,还会结束该窗口的进程并退出JVM。
更多推荐
所有评论(0)