windows 防止关闭Java swing应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2929483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent to close Java swing Application
提问by SamSol
How to prevent to close Java swing Application, when user clicks on close button?
当用户单击关闭按钮时,如何防止关闭 Java swing 应用程序?
回答by Gnoupi
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
on your main frame should prevent that.
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
在您的主框架上应该可以防止这种情况。
The setDefaultCloseOperation(int)method allows you to choose what to do when the user closes the JFrame:
该setDefaultCloseOperation(int)的方法可以让你选择当用户关闭的JFrame做什么:
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.
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):什么都不做;要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理操作。
HIDE_ON_CLOSE(在 WindowConstants 中定义):在调用任何注册的 WindowListener 对象后自动隐藏框架。
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):在调用任何注册的 WindowListener 对象后自动隐藏和处置框架。
EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit 方法退出应用程序。仅在应用程序中使用它。
回答by PeterMmm
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
// handle window closing
});