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