Nimbus Look And Feel adjust colors of menubar

前端 未结 2 1865
梦毁少年i
梦毁少年i 2021-01-06 03:35

I am trying to adjust the colors of the Nimbus Look and Feel but it is only working partially. Especially I have problems adjusting the colors of the menubar.

Here i

相关标签:
2条回答
  • 2021-01-06 04:17

    Making adjustLAF() static and invoking it before the Runnable seems to work on Mac OS X with com.apple.laf.AquaLookAndFeel.

    enter image description here

    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class JMenuColorTest extends JFrame {
    
        public static void main(String[] args) {
                    try {
                        adjustLAF();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
    
                    JMenuColorTest test = new JMenuColorTest();
                    test.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    test.setPreferredSize(new Dimension(400, 300));
                    test.pack();
                    test.setLocationRelativeTo(null);
    
                    JMenuBar menuBar = new JMenuBar();
                    JMenu menu1 = new JMenu("Menu 1");
                    menu1.add(new JMenuItem("Item 1.1"));
                    menu1.add(new JMenuItem("Item 1.2"));
                    menu1.add(new JMenuItem("Item 1.3"));
                    menuBar.add(menu1);
                    JMenu menu2 = new JMenu("Menu 2");
                    menu2.add(new JMenuItem("Item 2.1"));
                    menu2.add(new JMenuItem("Item 2.2"));
                    menu2.add(new JMenuItem("Item 2.3"));
                    menuBar.add(menu2);
                    test.setJMenuBar(menuBar);
    
                    test.setVisible(true);
                }
    
            });
        }
        private static void adjustLAF() throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
    
                    // Working
                    UIManager.put("control", Color.GREEN);
    
                    // Not working
                    UIManager.getLookAndFeelDefaults().put(
                        "MenuItem[Enabled].textForeground", Color.RED);
    
                    // Set the look and feel
                    UIManager.setLookAndFeel(info.getClassName());
    
                    // Not working
                    UIManager.put("control", Color.GREEN);
    
                    // Working
                    UIManager.getLookAndFeelDefaults().put(
                        "MenuItem[Enabled].textForeground", Color.RED);
    
                    break;
                }
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-01-06 04:20

    for JMenuBar to have to use Painter, to check NimbusDefault#value

    MenuBar[Enabled].backgroundPainter
    MenuBar[Enabled].borderPainter
    

    rest is in answer by trashgod +1

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