Change background and text color of JMenuBar and JMenu objects inside it

后端 未结 7 1597
孤街浪徒
孤街浪徒 2020-12-10 20:57

How can I set custom background color for JMenuBar and JMenu objects inside it? I tried .setBackgroundColor and it does not work!

相关标签:
7条回答
  • 2020-12-10 21:24

    Simple way to do is by .setBackground(Color.RED) and setOpaque(true)

    menubar.setBackground(Color.RED); menu.setBackground(Color.yellow); menubar.setOpaque(true); menu.setOpaque(true);

    This will give the color of your choices to both the menubar and menu.

    0 讨论(0)
  • 2020-12-10 21:31

    Mine only worked when I changed:

        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    

    to:

        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    

    Otherwise, the colors remained the same.

    0 讨论(0)
  • 2020-12-10 21:41

    Create a new class that extends JMenuBar:

    public class BackgroundMenuBar extends JMenuBar {
        Color bgColor=Color.WHITE;
    
        public void setColor(Color color) {
            bgColor=color;
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(bgColor);
            g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    
        }
    }
    

    Now you use this class instead of JMenuBar and set the background color with setColor().

    0 讨论(0)
  • 2020-12-10 21:45

    You would probably need to change opacity of menu items, ie:

    JMenuItem item= new JMenuItem("Test");
    item.setOpaque(true);
    item.setBackground(Color.CYAN);
    

    You can also achieve that globally using UIManager, for example:

    UIManager.put("MenuItem.background", Color.CYAN);
    UIManager.put("MenuItem.opaque", true);
    
    0 讨论(0)
  • 2020-12-10 21:45

    The simplest way (I can think of) is to change the default values used by the UIManager. This will effect all the menu bars and menu items in the application though...

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestMenuBar {
    
        public static void main(String[] args) {
            new TestMenuBar();
        }
    
        public TestMenuBar() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    UIManager.put("MenuBar.background", Color.RED);
                    UIManager.put("Menu.background", Color.GREEN);
                    UIManager.put("MenuItem.background", Color.MAGENTA);
    
                    JMenu mnu = new JMenu("Testing");
                    mnu.add("Menu Item 1");
                    mnu.add("Menu Item 2");
    
                    JMenuBar mb = new JMenuBar();
                    mb.add(mnu);
                    mb.add(new JMenu("Other"));
    
                    JFrame frame = new JFrame("Test");
                    frame.setJMenuBar(mb);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JPanel());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
    }
    
    0 讨论(0)
  • 2020-12-10 21:45
    public void run() {
    
         UIManager.put("MenuBar.background", new java.awt.Color(255, 245, 157));
         UIManager.put("MenuBar.opaque", true);                
         UIManager.put("Menu.background", new java.awt.Color(255, 245, 157));
         UIManager.put("Menu.opaque", true);
         UIManager.put("MenuItem.background",new java.awt.Color(255, 245, 157));
         UIManager.put("MenuItem.opaque", true);
         new MenuPrincipal().setVisible(true);
    
    }
    

    The menubar does not change color, but the rest do (menu and menuitem)

    0 讨论(0)
提交回复
热议问题