如何将 JMenuBar 移动到 Mac OS X 上的屏幕菜单栏? [英] How do I move my JMenuBar to the screen menu bar on Mac OS X?

查看:34
本文介绍了如何将 JMenuBar 移动到 Mac OS X 上的屏幕菜单栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 JMenuBar 移动到 Mac OS X 上的屏幕菜单栏时,它会在我的窗口中的菜单所在的位置留下一些空白区域;我需要删除那个空间.我正在使用

When I move my JMenuBar to the screen menu bar on Mac OS X, it leaves some blank space where the menu would be in my window; I need to remove that space. I am using

System.setProperty("apple.laf.useScreenMenuBar", "true")

将我的 JMenuBar 移动到屏幕菜单栏.我使用 Mac 的朋友报告说,如果我没有设置该属性,这会在菜单所在的位置留下一些难看的垂直空间.解决此问题的最佳方法是什么?

to move my JMenuBar to the screen menu bar. My friend who uses a Mac reports that this leaves some ugly vertical space where the menu would reside if I did not set that property. What is the best way to resolve this issue?

这是我的来源的一个例子:

Here is an example from my source:

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

推荐答案

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

Depending on when it's done, setting the property after your program launches may be too late to be effective. Instead, add the setting at launch time.

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

或者,在应用程序包的 Info.plist 中设置属性,如 Mac OS X 的 Java 部署选项Java 字典信息.plist 键关于 Info.plist KeysJava 运行时系统属性.

Alternatively, set the property in your application bundle's Info.plist, as discussed in Java Deployment Options for Mac OS X, Java Dictionary Info.plist Keys, About Info.plist Keys and Java Runtime System Properties.

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

附录:如下所示,使用@Urs Reupke 或我自己建议的方法不会出现问题.您(丢失的)DesktopMain 可能有问题.

Addendum: As shown below, the problem does not appear using the approach suggested by @Urs Reupke or myself. Your (missing) DesktopMain may be at fault.

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

这篇关于如何将 JMenuBar 移动到 Mac OS X 上的屏幕菜单栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆