问题描述
除了使用JNI之外,Java GUI应用程序是否有办法响应系统关闭或注销事件? (在Windows上,JNI会在Linux上使用WM_QUERYENDSESSION吗?)
Is there a way for a Java GUI application to respond to system shutdown or logoff events, other than to use JNI? (On Windows, the JNI would use WM_QUERYENDSESSION, on Linux?)
该方法应该允许程序提示用户保存等,然后继续注销过程。
The method should allow the program to prompt users to save, etc., and then continue the logoff process.
推荐答案
据我所知,Java无法捕获系统关闭或注销事件。
As far as I know, there's no way in Java to catch the system shutdown or logoff events.
但是,当JVM以。
AWT的还有一个windowClosing事件,你可以覆盖它挂钩到要监视的窗口。 Swing继承了这个;我相信SWT也是如此。请注意,如果覆盖此事件,必须手动处理窗口!
AWT's WindowAdapter also has a windowClosing event that you can override and hook to a window that you want to monitor. Swing inherits this; I believe SWT does as well. Be aware that you MUST manually dispose of the window if you override this event!
我相信MS Windows会在关闭时触发这些事件。我相信Linux / UNIX上的SIGTERM会做同样的事情,尽管如果这是在关机期间,Linux会很快在SIGKILL上发布应用程序。
I believe that MS Windows will fire these events as it is closing. I believe a SIGTERM on Linux/UNIX does the same, although Linux will SIGKILL an app shortly afterwords if this is during shutdown.
这篇关于Java:在Windows和Linux上处理注销或关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!