用Java创建NSStatusItem / Menubar应用程序 [英] Creating an NSStatusItem/Menubar App in Java

查看:172
本文介绍了用Java创建NSStatusItem / Menubar应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java模拟Objective C的NSStatusItem功能。也就是说,我正在尝试编写一个将放在Mac的菜单栏中的Java应用程序,喜欢这个以下是Apple在StatusBar上的文档的链接

I'm trying to emulate Objective C's NSStatusItem functionality in Java. That is, I'm trying to write a Java app that will sit in the Mac's menubar, like this. Here's a link to Apple's documentation on StatusBar.

用Java做任何事吗?

Any way to do this in Java?

推荐答案

使用< a href =http://docs.oracle.com/javase/6/docs/api/java/awt/SystemTray.html =nofollow noreferrer> java.awt.SystemTray 。我已经确认它适用于OS X Lion。根据这个问题,也可以使用SWT。

Use java.awt.SystemTray. I've confirmed it works on OS X Lion. According to this question, it's also possible to do with SWT.

示例:

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

public class MenuBarIconTest {
    public static void main(String[] args) throws MalformedURLException {
        TrayIcon trayIcon = null;
         if (SystemTray.isSupported()) {
             // get the SystemTray instance
             SystemTray tray = SystemTray.getSystemTray();
             // load an image
             Image image = Toolkit.getDefaultToolkit().getImage(new URL("http://cdn1.iconfinder.com/data/icons/Hypic_Icon_Pack_by_shlyapnikova/16/forum_16.png"));
             // create a action listener to listen for default action executed on the tray icon
             ActionListener listener = new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     System.out.println("action");
                     // execute default action of the application
                     // ...
                 }
             };
             // create a popup menu
             PopupMenu popup = new PopupMenu();
             // create menu item for the default action
             MenuItem defaultItem = new MenuItem("Do the action");
             defaultItem.addActionListener(listener);
             popup.add(defaultItem);
             /// ... add other items
             // construct a TrayIcon
             trayIcon = new TrayIcon(image, "Tray Demo", popup);
             // set the TrayIcon properties
             trayIcon.addActionListener(listener);
             // ...
             // add the tray image
             try {
                 tray.add(trayIcon);
             } catch (AWTException e) {
                 System.err.println(e);
             }
             // ...
         } else {
             // disable tray option in your application or
             // perform other actions
             //...
         }
         // ...
         // some time later
         // the application state has changed - update the image
         if (trayIcon != null) {
             //trayIcon.setImage(updatedImage);
         }

    }
}

这篇关于用Java创建NSStatusItem / Menubar应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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