根据完成的时间,在程序启动后设置属性可能为时已晚,无法生效.而是在启动时添加设置.
java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar
Properties
apple.laf.useScreenMenuBar
true
...
附录:如下所示,使用@Urs Reupke或我建议的方法不会出现问题.你的(缺失的)DesktopMain可能有问题.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
/** @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");
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
});
}
}