4

我正在尝试调整 Nimbus 外观和感觉的颜色,但它只是部分起作用。特别是我在调整菜单栏的颜色时遇到问题。

这是一个运行示例:

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) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    adjustLAF();
                } catch (Exception e) {
                    e.printStackTrace();
                }

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

            }
        });
    }
}

如您所见,我可以设置控件的背景并设置 JMenuItem 的前景色。但我无法更改 JMenuItem 的背景,也无法更改 MenuBar 的颜色。我从http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html尝试了很多键,但我无法更改菜单栏的颜色。

另一个问题是?为什么我必须在设置外观之前和设置外观之后调用一次颜色调整?为什么我必须调用一次'UIManager.put()'和一次'UIManager.getLookAndFeelDefaults().put()'?

在我看来,Nimbus 确实有问题,不适合专业使用。我尝试同时使用 JDK 1.6.35 和 JDK 1.7.7,但是对于这两个 JDK,我无法让系统按要求运行?

关于如何在 Nimbus LookAnd Feel 中调整菜单栏颜色的任何建议?

提前致谢

4

2 回答 2

5

因为JMenuBar必须使用Painter来检查NimbusDefault#value

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

休息的答案是trashgod +1

于 2012-09-11T12:53:32.967 回答
3

Mac adjustLAF()OS X 上使用.Runnablecom.apple.laf.AquaLookAndFeel

在此处输入图像描述

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

    }
}
于 2012-09-11T12:51:38.983 回答