一、贴上源码
public void setDefaultCloseOperation(int operation) {
if (operation != DO_NOTHING_ON_CLOSE &&
operation != HIDE_ON_CLOSE &&
operation != DISPOSE_ON_CLOSE &&
operation != EXIT_ON_CLOSE) {
throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
}
if (operation == EXIT_ON_CLOSE) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExit(0);
}
}
if (this.defaultCloseOperation != operation) {
int oldValue = this.defaultCloseOperation;
this.defaultCloseOperation = operation;
firePropertyChange("defaultCloseOperation", oldValue, operation);
}
}
1.1 官方的文档
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.
Params:
operation – the operation which should be performed when the user closes the frame
Throws:
SecurityException – if EXIT_ON_CLOSE has been specified and the SecurityManager will not allow the caller to invoke System.exit
IllegalArgumentException – if defaultCloseOperation value isn’t one of the above valid values
See Also:
addWindowListener, getDefaultCloseOperation, WindowConstants, Runtime.exit(int)j
1.2 翻译
设置用户在此框架上启动关闭时默认发生的操作。您必须指定以下选项之一:
DO_NOTHING_ON_CLOSE(在窗口常数中定义):不要执行任何操作;要求程序处理注册窗口监听器对象的窗口关闭方法中的操作。
HIDE_ON_CLOSE(在窗口常数中定义):调用任何注册的窗口监听器对象后自动隐藏帧。
DISPOSE_ON_CLOSE(在窗口常数中定义):在调用任何已注册的窗口监听器对象后,自动隐藏和处理该帧。
EXIT_ON_CLOSE(在JFrame中定义):使用“系统退出”方法退出应用程序。仅在应用程序中使用它。
默认情况下,将该值设置为HIDE_ON_CLOSE。对此属性值的更改会导致属性更改事件的触发,且属性名称为默认关闭操作。
注意:当Java虚拟机(VM)中的最后一个可显示窗口被处理时,VM可能会终止。有关详细信息,请参见AWT线程问题。
参数:
操作-当用户关闭帧时应执行的操作
线:
安全异常——如果已经指定了EXIT_ON_CLOSE,并且安全管理器将不允许调用者调用System.exit
异常-如果默认关闭操作值不是上述有效值之一
参见:
addWindowListener,getDefaultCloseOperation,WindowConstants,Runtime.exit(int)
二、关于 DefaultCloseOperation
用户单击窗口关闭按钮时执行的操作
2.1 DO_NOTHING_ON_CLOSE(0)
不执行任何操作;要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理该操作,用户单击窗口关闭按钮时无法关闭这个窗口。
2.2 HIDE_ON_CLOSE(1)
调用任意已注册的 WindowListener 对象后自动隐藏该窗体。
2.3 DISPOSE_ON_CLOSE(2)
调用任意已注册 WindowListener 的对象后自动隐藏并释放该窗体。
2.4 EXIT_ON_CLOSE(3)
使用 System exit 方法退出应用程序。仅在应用程序中使用。
2.5 默认值
默认情况下,该值被设置为 HIDE_ON_CLOSE。
三、调用示例
JFrame frame = new JFrame();
top.setDefaultCloseOperation(3);