4

在 Java Swing 应用程序中,我正在尝试 nimbus 的外观和感觉。它在 JdesktopPane 控件中看起来很棒,但我希望我的所有桌面窗格具有不同的颜色,但主题很好。

有什么方法可以改变 Nimbus 外观的背景颜色吗?

这是应用 nimbus 外观的示例代码。

   try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(info.getName())) {
          UIManager.setLookAndFeel(info.getClassName());
          break;
         }
        }
       }  
     catch (Exception e) {}
4

2 回答 2

3
UIManager.put("nimbusBase", new Color(...));
UIManager.put("nimbusBlueGrey", new Color(...));
UIManager.put("control", new Color(...)) 

来自l&f 教程

于 2013-02-05T05:43:47.757 回答
2

Nimbus 使用看起来像矢量类型的绘图例程来绘制背景,以绘制精美的背景图案。要在 nimbus 的情况下更改 JDesktopPane 的背景,您需要更改 JDesktopPane 使用的背景 Painter 以简单地用您需要的背景颜色(例如灰色)填充窗格。然后"DesktopPane[Enabled].backgroundPainter"使用该 Painter 对象设置属性。例如观看下面给出的代码:

import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import com.sun.java.swing.Painter;
import java.awt.Color;

public class NimbusFrame extends JFrame
{
    private JDesktopPane desktop;
    public void prepareAndShowGUI()
    {
        desktop = new MyDesktopPane();
        getContentPane().add(desktop);
        setSize(300,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    class MyDesktopPane extends JDesktopPane
    {
        @Override
        public void updateUI() 
        {
            if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) 
            {
                UIDefaults map = new UIDefaults();
                Painter<JComponent> painter = new Painter<JComponent>() 
                {
                    @Override
                    public void paint(Graphics2D g, JComponent c, int w, int h) 
                    {
                        g.setColor(Color.gray);
                        g.fillRect(0, 0, w, h);
                    }
                };
                map.put("DesktopPane[Enabled].backgroundPainter", painter);
                putClientProperty("Nimbus.Overrides", map);
            }
            super.updateUI();
        }
    }
    public static void main(String st[])
    {
        try
        {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
            {
                    if ("Nimbus".equalsIgnoreCase(info.getName())) 
                {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
        }catch(Exception ex){}
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                NimbusFrame frame = new NimbusFrame();
                frame.prepareAndShowGUI();
            }
        });
    }
}
于 2013-02-05T07:30:35.560 回答