简体   繁体   中英

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

I am building a Mac desktop application in java. I want to change the background of a JButton and also I want to make my JMenuBar be in the top.

To put my JMenuBar on the top I added this code:

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

And it worked!

And to change the backgroud color I used this:

    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) {

    }

And it also worked!

The problem was that when I changed the color the JMenuBar was not in the top. After a little debugging I know that changing the LookAndFeel is the responsable.

The complete code:

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

So this code change the color of the button but the JMenuBar is not in the top. If you comment the lines in the try it will not change the color but it will put the JMenuBar on the top.

Any help??

Thanks in advance!

I solved this by adding the line

b.setUI(new MetalButtonUI());

and deleting the try catch.

It ended up looking like these:

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

You might try a JMenu and a JMenuItem. This way you can multiple 'menu items' to a menu:

....

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

....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM