how to change background color for nimbus look and feel using java

做~自己de王妃 提交于 2019-12-23 20:08:39

问题


In Java Swing application, I am trying nimbus look and feel. It looks excellent in JdesktopPane control but i want the different color for my all desktoppane but theme is fine.

Is there any way to change the background color of nimbus look and feel?

Here is the sample code to apply the nimbus look and feel.

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

回答1:


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

from the l&f tutorial




回答2:


Nimbus paints the background using what looks like vector type drawing routines to paint a fancy background pattern. To change the background of JDesktopPane in case of nimbus you need to chang the background Painter used by the JDesktopPane to simply fill the pane with the background color that you need (say gray).And then set the "DesktopPane[Enabled].backgroundPainter" propery with that Painter object. For example watch the code given below:

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


来源:https://stackoverflow.com/questions/14700758/how-to-change-background-color-for-nimbus-look-and-feel-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!