简体   繁体   中英

Getting the OS X App Name Menu in Java

I've successfully mastered the ability to move my JMenuBar s to the OS X system menu bar. However, when I do this, I am presented with my JMenuBar preceded by both the Apple logo and the name of my app (Say, MyApp ). I want to be able to directly access the MyApp menu so that I can add JMenuItem s such as Preferences and Restart. How can I access this MyApp menu?

Here is my code so far:

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:
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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