2012-01-21 33 views
5

当我将我的JMenuBar移动到Mac OS X上的屏幕菜单栏时,它会留下一些空白区域,菜单位于我的窗口中;我需要删除那个空间。我正在使用如何将JMenuBar移动到Mac 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); 
} 
+1

你可以张贴代码?通常情况下,布局会自动调整以使用所有房间。 – Eelke

回答

11

根据当它完成时,设置属性后您的程序启动可能为时已晚才有效。相反,请在启动时添加设置。

java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar 

或者,在你的应用程序包的Info.plist设置属性,如Java Deployment Options for Mac OS XJava Dictionary Info.plist KeysAbout Info.plist KeysJava Runtime System Properties讨论。

<key>Properties</key> 
<dict> 
    <key>apple.laf.useScreenMenuBar</key> 
    <string>true</string> 
    ... 
</dict> 

附录:如下图所示,这个问题确实不使用通过@Urs Reupke或自己建议的方法出现。您的(丢失的)DesktopMain可能有问题。

Screen capture

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); 
      } 
     }); 
    } 
} 
+0

trashgod,这看起来不正确 - 我在我的代码中执行它,但我还没有听到投诉。 –

+0

@UrsReupke:好点;编辑。我无法重现该效果,但我看到过[this]这样的报告(http://stackoverflow.com/a/1654547/230513)。 – trashgod

+0

+1。 –

3

作为替代暗示什么trashgod,你在做什么 - 但这样做非常早期的,你初始化UI之前。

此外,你可能会考虑使用该酒吧以显示您的应用程序的名称:

System.setProperty("com.apple.mrj.application.apple.menu.about.name", "MyApplication"); 
+0

+1 tnecniv:请提供一个[sscce](http://sscce.org/),展示您描述的问题。 – trashgod

+0

@trashgod我添加了我的源示例 – tnecniv

+0

@tnecniv:我详细阐述了[这里](http://stackoverflow.com/a/8956715/230513)。 – trashgod

0

对于Java 8使用本

System.setProperty("apple.awt.application.name", "My app");