简体   繁体   中英

How do I modify the auto-generated menu and “About” box of Mac LAF in Java?

在此处输入图片说明

The screenshot shows on the menu bar an apple (which is the system menu) the app menu "GSPro" which has the about and quit items, the ss also shows how the built in about box looks, this can be modified to look more professional and have icons/revs/author etc. That's the bits I'm having trouble finding info on and good examples. The file menu onwards is what my app generates and will look the same in windows so I know I would have to do windows menus slightly differently from mac stuff.

On the mac this is known as laf (look and feel).

My code snip is as follows, no handlers yet but just testing how it shows...

    public class GSPro {

/**
 * @param args
 */
public static void main(String[] args) {

    System.setProperty("apple.laf.useScreenMenuBar", "true");

    JFrame frame = new JFrame();
    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setTitle("GSPro");

    JMenuBar menuBar = new JMenuBar();

    JMenu file = new JMenu("File");
    menuBar.add(file);
    JMenu edit = new JMenu("Edit");
    menuBar.add(edit);
    JMenuItem  exit = new JMenuItem("New");
    file.add(exit);
    JMenuItem open = new JMenuItem("Open");
    file.add(open);
    file.addSeparator();
    JMenuItem close = new JMenuItem("Close");
    file.add(close);
    file.addSeparator();
    JMenuItem save = new JMenuItem("Save");
    file.add(save);
    JMenuItem saveas = new JMenuItem("Save As...");
    file.add(saveas);

    frame.setJMenuBar(menuBar);
}

}

As far as the icon and version information, it comes from the application bundle properties file (Info.plist). Create an application bundle, complete with icon and version properties and you'll see the About dialog display this info.

You might be better off making your own custom About dialog though. I've never seen an application that used the default one as you screen captured above. Additionally, this would allow you to use the same About dialog across platforms. Here's how you would do it on OS X:

public class Test implements com.apple.eawt.AboutHandler {

    public Test() {
        // comment these two lines to see the default About dialog
        com.apple.eawt.Application app = com.apple.eawt.Application.getApplication();
        app.setAboutHandler(this);

        JFrame myFrame = new JFrame();
        myFrame.setSize(200, 200);
        myFrame.setVisible(true);
    }



    @Override
    public void handleAbout(com.apple.eawt.AppEvent.AboutEvent ae) {
        JFrame aboutFrame = new JFrame();
        aboutFrame.setSize(200, 200);
        aboutFrame.add(new JLabel("About"));
        aboutFrame.pack();
        aboutFrame.setVisible(true);
    }



    public static void main(String[] args) {
        new Test();
    }
}

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