macos 使用 Java 的 Mac Os 中的系统托盘(菜单附加)图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1970876/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:38:15  来源:igfitidea点击:

System Tray (Menu Extras) icon in Mac Os using Java

javamacosdesktop-applicationsystem-tray

提问by sinuhepop

I'm developing a desktop application using Java. I want to put an icon (with a contextual menu) on the system tray (called Menu Extras in Mac Os). Java 6 comes with support for doing this in Windows and Linux, but it doesn't work in Mac Os.

我正在使用 Java 开发桌面应用程序。我想在系统托盘上放置一个图标(带有上下文菜单)(在 Mac Os 中称为 Menu Extras)。Java 6 支持在 Windows 和 Linux 中执行此操作,但在 Mac Os 中不起作用。

I have seen some applications doing what I want in all three operating systems (e.g. DropBox), but I don't know if they are made with Java.

我已经看到一些应用程序在所有三个操作系统(例如 DropBox)中都在做我想做的事,但我不知道它们是否是用 Java 制作的。

How can I achieve this?

我怎样才能做到这一点?

If it's not possible in Java, is there any other cross-platform language able to do that?

如果在 Java 中不可能,是否还有其他跨平台语言能够做到这一点?

Thanks.

谢谢。

回答by Powerlord

AWT / Swing

AWT / 摆动

According to documentation, OSX 10.5 update 1 and newer support TrayIcons

根据文件,OSX 10.5 Update 1和较新的支持TrayIcon小号

TrayIcons are represented on Mac OS X using NSStatusMenus that are presented to the left of the standard system menu extras. The java.awt.Image artwork for a TrayIcon is presented in grayscale as per the Mac OS X standard for menu extras.

TrayIcon.displayMessage() presents a small non-modal dialog positioned under the TrayIcon. The ActionListener for the TrayIcon is only fired if the "OK" button on the non-modal dialog is pressed, and not if the window is closed using the window close button.

Multiple calls to TrayIcon.displayMessage() will dismiss prior messages and leave only the last message. If the application is not in the foreground when TrayIcon.displayMessage() is called, the application bounces its icon in the Dock. Message windows are badged with the application's icon to identify the which application triggered the notification.

TrayIcons 在 Mac OS X 上使用 NSStatusMenus 表示,这些 NSStatusMenus 出现在标准系统菜单附加项的左侧。TrayIcon 的 java.awt.Image 图稿按照 Mac OS X 菜单附加标准以灰度显示。

TrayIcon.displayMessage() 呈现一个位于 TrayIcon 下方的小型非模态对话框。TrayIcon 的 ActionListener 仅在非模态对话框上的“确定”按钮被按下时才会被触发,并且在使用窗口关闭按钮关闭窗口时不会被触发。

多次调用 TrayIcon.displayMessage() 将关闭之前的消息并只留下最后一条消息。如果调用 TrayIcon.displayMessage() 时应用程序不在前台,则应用程序会在 Dock 中弹回其图标。消息窗口标有应用程序的图标,以标识哪个应用程序触发了通知。

noahprovided this sample:

诺亚提供了这个样本:

java.awt.SystemTray.getSystemTray().add(new java.awt.TrayIcon(java.awt.Toolkit.getDefaultToolkit().getImage("foo.png")));

Note that you'll probably want to attach a menu to that icon before adding it to the tray, though.

请注意,在将其添加到托盘之前,您可能希望将菜单附加到该图标。

SWT

声波

According to documentation, SWT 3.3 and newer supports TrayItemicons on OSX.

根据文档,SWT 3.3 和更新版本支持OSX 上的TrayItem图标。

Icons placed on the system tray will now appear when running on OS X in the status bar. SWT TrayItem

在 OS X 上运行时,系统托盘上的图标现在将出现在状态栏中。 SWT 托盘项目

This snippetshows how to create a menu and icon and put them in the Tray.

此代码段展示了如何创建菜单和图标并将它们放入托盘中。

回答by Mike

I ported a Windows application to my Mac with little difficulty. One thing I noticed is that the icons are in full, living color (not following the Mac convention). I'll need to add a little OS-specific code to convert myself. But this is a big step up from the DLL dependent Desktop integration version from earlier iterations of Java.

我毫不费力地将一个 Windows 应用程序移植到我的 Mac 上。我注意到的一件事是图标是完整的、生动的颜色(不遵循 Mac 惯例)。我需要添加一些特定于操作系统的代码来转换自己。但这比 Java 早期迭代的依赖于 DLL 的桌面集成版本有了很大的进步。