在Java中获取OS X App Name菜单

时间:2013-02-20 19:46:45

标签: java macos system-integration

我已经成功掌握了将JMenuBar移动到OS X系统菜单栏的功能。但是,当我这样做时,我会看到我的JMenuBar前面有Apple徽标和我的应用程序名称(Say, MyApp )。我希望能够直接访问 MyApp 菜单,以便添加JMenuItem,例如“首选项”和“重新启动”。 如何访问此 MyApp 菜单?

到目前为止,这是我的代码:

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Clazz extends JFrame
{
    public Clazz()
    {
        setUpGUI();
    }

    private JMenuBar menuBar;
    private JMenu appMenu;
    private void setUpGUI()
    {
        setUsesSystemMenuBar(true, "MyApp");
        if ((menuBar = getJMenuBar()) == null)
        {
            menuBar = new JMenuBar();
            setJMenuBar(menuBar);
        }
        if ((appMenu = getSystemAppMenu()) == null)
            appMenu = new JMenu();
        appMenu.add(new JMenuItem("My Menu Item");
    }

    private void setUsesSystemMenuBar(boolean uses, String appName)
    {
        System.setProperty("apple.laf.useScreenMenuBar", Boolean.toString(uses));
        if (name != null)
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", appName.toString());
    //Will put code in for Ubuntu Unity, too, once I figure that out
    }

    private JMenu getSystemAppMenu()
    {
        //I don't know what to put here! D:
    }
}

1 个答案:

答案 0 :(得分:2)