【问题标题】:Creating an NSStatusItem/Menubar App in Java在 Java 中创建一个 NSStatusItem/Menubar 应用程序
【发布时间】:2012-11-09 00:14:38
【问题描述】:

我正在尝试在 Java 中模拟 Objective C 的 NSStatusItem 功能。也就是说,我正在尝试编写一个 Java 应用程序,它将位于 Mac 的菜单栏中,like thisHere's a link Apple 在 StatusBar 上的文档。

在 Java 中有什么方法可以做到这一点?

【问题讨论】:

    标签: java macos statusbar menubar


    【解决方案1】:

    使用java.awt.SystemTray。我已经确认它可以在 OS X Lion 上运行。根据this question,也可以使用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);
             }
    
        }
    }
    

    【讨论】:

    【解决方案2】:

    有一个 NICE 库可以做到这一点……我见过的最好的库,它将 SWIFT 功能与 JavaFX 的菜单栏访问集成在一起……因为 FX 没有本地方法来处理菜单栏。

    Here is the link如果你有兴趣。

    【讨论】:

      猜你喜欢
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多