【问题标题】:How do I put the JMenuBar on top in mac and change the background of a JButton如何在 mac 中将 JMenuBar 放在顶部并更改 JButton 的背景
【发布时间】:2015-09-21 10:21:26
【问题描述】:

我正在用 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 放在顶部。

有什么帮助吗??

提前致谢!

【问题讨论】:

  • setLookAndFeel 通常也属于 main 的开头 - 在创建任何 GUI 之前。
  • 所以,您要求 MacOS 将您的菜单栏放在系统菜单栏中,但是,您正在尝试使用跨平台的外观和感觉......而您没有看到那里有冲突?
  • @MadProgrammer 老实说,我不知道什么是跨平台的外观和感觉(甚至不知道外观和感觉是什么),我只是用谷歌搜索了如何更改 JButton 的背景,那是我创立的。如果您能帮我解决这些问题,我将不胜感激。
  • 你要使用系统观感,跨平台观感要使用金属(:P),你可以通过使用使按钮在Mac OS下工作,并将其传递给false

标签: java macos swing jmenubar


【解决方案1】:

我通过添加行解决了这个问题

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

【讨论】:

    【解决方案2】:

    您可以尝试 JMenu 和 JMenuItem。这样您就可以将多个“菜单项”添加到一个菜单中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-05
      • 2020-12-06
      • 1970-01-01
      • 2014-05-11
      • 2016-05-08
      • 2014-05-28
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多