简体   繁体   English

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

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

I'm developing a desktop application using Java. 我正在使用Java开发桌面应用程序。 I want to put an icon (with a contextual menu) on the system tray (called Menu Extras in Mac Os). 我想在系统托盘上放置一个图标(带有上下文菜单)(在Mac OS中称为菜单附加功能)。 Java 6 comes with support for doing this in Windows and Linux, but it doesn't work in Mac Os. Java 6支持在Windows和Linux中执行此操作,但它在Mac Os中不起作用。

I have seen some applications doing what I want in all three operating systems (eg 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. 谢谢。

AWT / Swing AWT / Swing

According to documentation , OSX 10.5 update 1 and newer support TrayIcon s 根据文档 ,OSX 10.5更新1和更新版本支持TrayIcon

TrayIcons are represented on Mac OS X using NSStatusMenus that are presented to the left of the standard system menu extras. TrayIcons在Mac OS X上使用NSStatusMenus表示,它们显示在标准系统菜单附加功能的左侧。 The java.awt.Image artwork for a TrayIcon is presented in grayscale as per the Mac OS X standard for menu extras. TrayIcon的java.awt.Image图稿按照Mac OS X标准的灰度显示,用于菜单附加功能。

TrayIcon.displayMessage() presents a small non-modal dialog positioned under the TrayIcon. TrayIcon.displayMessage()提供了一个位于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. 如果按下非模态对话框上的“确定”按钮,则仅触发TrayIcon的ActionListener,而不是使用窗口关闭按钮关闭窗口时触发。

Multiple calls to TrayIcon.displayMessage() will dismiss prior messages and leave only the last message. 对TrayIcon.displayMessage()的多次调用将关闭先前的消息并仅保留最后一条消息。 If the application is not in the foreground when TrayIcon.displayMessage() is called, the application bounces its icon in the Dock. 如果在调用TrayIcon.displayMessage()时应用程序不在前台,则应用程序会在Dock中弹回其图标。 Message windows are badged with the application's icon to identify the which application triggered the notification. 使用应用程序的图标标记消息窗口,以标识触发通知的应用程序。

noah provided 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 SWT

According to documentation , SWT 3.3 and newer supports TrayItem icons 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. 现在,在状态栏中的OS X上运行时,将显示放置在系统托盘上的图标。 SWT TrayItem

This snippet shows how to create a menu and icon and put them in the Tray. 此代码段显示如何创建菜单和图标并将其放入托盘中。

I ported a Windows application to my Mac with little difficulty. 我将一个Windows应用程序移植到我的Mac上并没有什么困难。 One thing I noticed is that the icons are in full, living color (not following the Mac convention). 我注意到的一件事是图标是完整的,生动的颜色(不遵循Mac惯例)。 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. 但这是从早期Java迭代的DLL依赖桌面集成版本迈出的一大步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM