【问题标题】:How do I move my JMenuBar to the screen menu bar on Mac OS X?如何将我的 JMenuBar 移动到 Mac OS X 上的屏幕菜单栏?
【发布时间】:2012-02-15 20:36:02
【问题描述】:

当我将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);
}

【问题讨论】:

  • 你能发布代码吗?通常布局会自动调整以使用所有房间。

标签: java macos swing jmenubar


【解决方案1】:

根据完成的时间,在您的程序启动后设置属性 可能为时已晚而无法生效。而是在启动时添加设置。

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 可能有问题。

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

【讨论】:

  • trashgod,这似乎不对 - 我正在我的代码中这样做,但我还没有听到投诉。
  • @UrsReupke:好点;编辑。我无法重现效果,但我看到过类似this 的报告。
【解决方案2】:

作为垃圾神建议的替代方案,做你正在做的事情 - 但在初始化你的 UI 之前尽早做。

另外,您可以考虑使用它在栏中显示应用程序的名称:

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

【讨论】:

  • +1 tnecniv:请提供一个sscce,表明您描述的问题。
  • @trashgod 我添加了我的源示例
【解决方案3】:

我知道这个问题很老,但对我有用的是我创建了一个名为 Main 的新类,然后将系统属性设置为

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

适用于 JDK15。

【讨论】:

    【解决方案4】:

    对于 Java 8 使用这个

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

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2011-02-10
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多