在OSX上摇摆:如何捕获命令-Q? [英] Swing on OSX: How to Trap command-Q?

查看:150
本文介绍了在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.

这篇关于在OSX上摇摆:如何捕获命令-Q?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆