当我将其移至JMenuBarMac OS X上的屏幕菜单栏时,它将在菜单窗口中留出一些空白。我需要删除该空间。我在用
System.setProperty("apple.laf.useScreenMenuBar", "true")
将我JMenuBar移到屏幕菜单栏。我的一位使用Mac的朋友报告说,如果我没有设置该属性,则会在菜单上保留一些丑陋的垂直空间。解决此问题的最佳方法是什么?
编辑:这是我的来源的一个示例:
public static void main(String[] args) { System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Name"); JFrame frame = new JFrame("Gabby"); final DesktopMain dm = new DesktopMain(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(dm); frame.setSize(160, 144); frame.setLocationRelativeTo(null); frame.setIgnoreRepaint(true); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); // Populating the menu bar code goes here frame.setJMenuBar(menuBar); frame.setVisible(true); }
根据完成的时间,在程序启动后设置属性可能为时已晚,无法生效。而是在启动时添加设置。
java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar
或者,Info.plist如Mac OS X的Java部署选项,Java词典Info.plist键,关于Info.plist键和Java运行时系统属性所述,在应用程序捆绑包中设置属性。
Info.plist
<key>Properties</key> <dict> <key>apple.laf.useScreenMenuBar</key> <string>true</string> ... </dict>
附录:如下所示,使用@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 http://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); } }); } }