在OSX上摇摆:如何捕获命令-Q?

在OSX上摇摆:如何捕获命令-Q?

问题描述:

确信(教育)Mac上的Swing应用看起来是原生的,我试图让我看起来尽可能本地。一切看起来都很棒,但当我点击命令 + Q 或从菜单中执行此操作时,我的 windowStateChanged(WindowEvent e)没有在我的主JFrame上触发(如果我以任何其他方式退出,它会触发)。我如何回应真正的Apple退出?

After being convinced ("schooled") that Swing apps on Mac do look native, I'm trying to make mine look as native as possible. Everything looks great, but when I hit command+Q or do it from the menu, my windowStateChanged(WindowEvent e) is not firing on my main JFrame (if I exit in any other way, it does fire). How can I respond to the real Apple quit?

最高投票答案非常好,但只是填写最佳方式:

The top voted answer is excellent but just to fill in the "best way":

System.setProperty("apple.eawt.quitStrategy", "CLOSE_ALL_WINDOWS");

这将触发标准窗口关闭回调事件,这对于可移植代码应该非常好用。

This will trigger the standard window closing callback event which should work really nicely for portable code.

作为下面讨论的结果,似乎在应用程序的早期做到这一点至关重要。在执行任何UI代码之前,我在主类的静态初始化程序中尽早编写了这个。

As a result of the discussion below it seems that its crucial to do this really early in the app. I wrote this early in the static initializer of the main class before any UI code was executed.