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

查看:24
本文介绍了在 OSX 上摇摆:如何捕获命令 Q?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确信(受过教育")Mac 上的 Swing 应用确实看起来很原生之后,我试图让我的看起来尽可能的本土化.一切看起来都不错,但是当我点击 command+Q 或从菜单中执行此操作时,我的 windowStateChanged(WindowEvent e) 没有在我的main 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天全站免登陆