简体   繁体   中英

Java Swing GUI in Mac OS X 10.9

After implementing Mac OS X look and feel, just after reading

I noticed two problems.

  1. While the JMenuBar is now shown in the Mac Bar, if I click on a JMenuItem , no event is called. Using: System.setProperty("apple.laf.useScreenMenuBar", "true");
  2. The name shown in the bar using System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Name"); is the name given in the cration of the project.

The interface:图形用户界面


I'm trying Java OS X Lion Set application name doesn't work to solve the second problem. I' can't launch the jar from command line, so I'm using Martjin answer.

Starting from the example cited ,

  1. The JMenuItem listener prints "Here" when File > Item is clicked.

  2. The about.name property is the name in the About dialog, shown below, but the property is currently ignored.

图片

As tested:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/22766921/230513
 * @see https://stackoverflow.com/questions/8955638
 */
public class NewMain {

    public static void main(String[] args) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty(
            "com.apple.mrj.application.apple.menu.about.name", "Name");
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                JFrame frame = new JFrame("Gabby");
                final JPanel dm = new JPanel() {

                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(320, 240);
                    }
                };
                dm.setBorder(BorderFactory.createLineBorder(Color.blue, 10));

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(dm);
                frame.pack();
                frame.setLocationByPlatform(true);

                JMenuBar menuBar = new JMenuBar();
                JMenu fileMenu = new JMenu("File");
                JMenuItem item = new JMenuItem("Item");
                fileMenu.add(item);
                item.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Here");
                    }
                });
                menuBar.add(fileMenu);
                frame.setJMenuBar(menuBar);
                frame.setVisible(true);
            }
        });
    }
}

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