通过 unsing netbeans ide,我JDesktopPane
在JFrame
. 而且我无法更改 jdesktopPane 的颜色。我尽我所能。但是当我打开JFrame
..JDesktopPane
里面JFrame
是一些蓝色的背景。
请帮我改变背景JDesktopPane
通过 unsing netbeans ide,我JDesktopPane
在JFrame
. 而且我无法更改 jdesktopPane 的颜色。我尽我所能。但是当我打开JFrame
..JDesktopPane
里面JFrame
是一些蓝色的背景。
请帮我改变背景JDesktopPane
我将假设您正在使用具有默认 Nimbus 外观和感觉的 GUI Builder(因为您说您已经尝试了所有方法,我假设您已经尝试过setBackground
)。外观具有背景设置。但是你有围绕它的选择。
你可以只画背景。您想查看此答案以了解如何编辑自动生成的代码。然后你可以在编辑代码时做到这一点。之后不要忘记点击++
ctrl,以解决所有导入问题。我懒得写完全限定的名字。shiftI
jDesktopPane1 = new javax.swing.JDesktopPane() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
如果你想要一个图像,你可以画一个图像
jDesktopPane1 = new javax.swing.JDesktopPane() {
private Image image;
{
try {
image = ImageIO.read(new URL("http://www.hdbackgroundspoint.com/wp-content/uploads/2013/12/16/345t34.jpeg"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
};
您还可以覆盖 Nimbus 默认值DesktopPane[Enabled].backgroundPainter
。在此处查看Nimbus 默认值
public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo laf : UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
UIManager.getLookAndFeelDefaults().put(
"DesktopPane[Enabled].backgroundPainter",
new DesktopPainter());
}
}
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDesktopPaneDemo();
}
});
}
static class DesktopPainter implements Painter<JComponent> {
private Image image;
public DesktopPainter() {
try {
image = ImageIO.read(new URL("http://www.hdbackgroundspoint.com/wp-content/uploads/2013/09/hh.jpeg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.drawImage(image, 0, 0, width, height, null);
}
}