在Java Swing应用程序中,我正在尝试灵气的外观和感觉。它在JdesktopPane控件中看起来很棒,但我希望我的所有desktoppane都有不同的颜色,但主题很好。
有没有办法改变灵气外观的背景颜色?
以下是应用nimbus外观的示例代码。
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e) {}
答案 0 :(得分:3)
UIManager.put("nimbusBase", new Color(...));
UIManager.put("nimbusBlueGrey", new Color(...));
UIManager.put("control", new Color(...))
来自l&f tutorial 的
答案 1 :(得分:2)
Nimbus使用看起来像矢量类型绘制例程来绘制背景来绘制花哨的背景图案。
要在nimbus的情况下更改JDesktopPane的背景,您需要更改JDesktopPane使用的背景Painter,只需用您需要的背景颜色(例如灰色)填充窗格。然后将"DesktopPane[Enabled].backgroundPainter"
属性设置为画家对象。
例如,观看下面给出的代码:
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();
}
});
}
}