问题
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