[英]How to change the background color for JPanels with Nimbus Look and Feel?
[英]how to change background color for nimbus look and feel using java
在Java Swing应用程序中,我正在尝试灵气的外观和感觉。 它在JdesktopPane控件中看起来很棒,但我希望我的所有desktoppane都有不同的颜色,但主题很好。
有没有办法改变灵气的外观和感觉的背景颜色?
以下是应用nimbus外观的示例代码。
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e) {}
UIManager.put("nimbusBase", new Color(...));
UIManager.put("nimbusBlueGrey", new Color(...));
UIManager.put("control", new Color(...))
来自l&f教程
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();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.