7

Mac OS X でスクリーン メニュー バーに移動するJMenuBarと、ウィンドウ内でメニューが表示される場所に空白のスペースが残ります。そのスペースを削除する必要があります。私は使っている

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

4 に答える 4

12

完了時期によっては、プログラムの起動にプロパティを設定しても効果が得られない場合があります。代わりに、起動時に設定を追加してください。

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

または、 Mac OS X の Java デプロイメント オプションJava Dictionary 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);
            }
        });
    }
}
于 2012-01-21T21:11:26.080 に答える
4

Trashgod の提案に代わる方法として、現在行っていることを行いますが、UI を初期化する前の非常に早い段階で行います。

また、これを使用してアプリケーションの名前をバーに表示することを検討することもできます。

    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "MyApplication");
于 2012-01-21T21:21:56.047 に答える