如何将 JMenuBar 放在 mac 的顶部并更改 JButton 的背景

How do I put the JMenuBar on top in mac and change the background of a JButton

我正在 java 中构建一个 Mac 桌面应用程序。我想更改 JButton 的背景,还想让我的 JMenuBar 位于顶部。

为了将我的 JMenuBar 放在顶部,我添加了以下代码:

  System.setProperty("apple.laf.useScreenMenuBar", "true");
  System.setProperty(
      "com.apple.mrj.application.apple.menu.about.name", "Stack");

成功了!

为了改变背景颜色,我使用了这个:

    JButton b = new JButton("press me!");

    b.setBackground(Color.blue);

    b.setContentAreaFilled(false);
    b.setOpaque(true);
    b.setBorderPainted(true);
    try {
        UIManager.setLookAndFeel(UIManager
                .getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) {

    }

而且它也有效!

问题是当我更改颜色时,JMenuBar 不在顶部。经过一些调试后,我知道更改 LookAndFeel 是负责任的。

完整代码:

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.UIManager;

public class Main {
    public static void main(String[] args) {    
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty(
          "com.apple.mrj.application.apple.menu.about.name", "Stack");

        JButton b = new JButton("press me!");           
        b.setBackground(Color.blue);    
        b.setContentAreaFilled(false);
        b.setOpaque(true);
        b.setBorderPainted(true);
        try {
            UIManager.setLookAndFeel(UIManager
                    .getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {

        }   
        JFrame f = new JFrame();            
        f.add(b);           
        JMenuBar m = new JMenuBar();            
        m.add(new JMenu("item"));   
        f.setJMenuBar(m);
        f.setVisible(true);         
        f.setVisible(true);
    }
}

因此这段代码更改了按钮的颜色,但 JMenuBar 不在顶部。如果您在 try 中注释行,它不会改变颜色,但会将 JMenuBar 放在顶部。

有帮助吗??

提前致谢!

您可以尝试使用 JMenu 和 JMenuItem。这样你就可以将多个'menu items'添加到一个菜单中:

....

menubar = new JMenuBar();

menu1 = new JMenu();

menuItem1 = new JMenuItem("item 1");

// add menu item to menu
menu1.add(menuItem1);

// add menu to menubar
menubar.add(menu1);

....

我通过添加行

解决了这个问题
b.setUI(new MetalButtonUI());

并删除 try catch。

最终看起来像这样:

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalButtonUI;

public class Main {

    public static void main(String[] args) {

        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("com.apple.mrj.application.apple.menu.about.name",
                "Stack");
        JButton b = new JButton("press me!");

        b.setBackground(Color.blue);

        b.setContentAreaFilled(false);
        b.setOpaque(true);
        b.setBorderPainted(true);
        b.setUI(new MetalButtonUI());


        JFrame f = new JFrame();

        f.add(b);



        f.setBounds(0, 0, 500, 500);
        JMenuBar m = new JMenuBar();

        m.add(new JMenu("item"));



        f.setJMenuBar(m);
        f.setVisible(true);

        f.setVisible(true);
    }
}