sha*_*999 4 java swing netbeans background-color jdesktoppane
通过unsing NetBeans IDE中,我创建了一个JDesktopPane
内部的JFrame
。并且我无法更改jdesktopPane的颜色。但是,当我打开JFrame
.. JDesktopPane
内部时,JFrame
处于某种蓝色背景中。
请帮我改变背景 JDesktopPane
我将假设您使用具有默认Nimbus外观的GUI Builder(因为您说您已经尝试了所有操作,而我会假设您已经尝试过setBackground
)。外观具有背景设置。但是您有很多选择。
您可以只绘制背景。您想看一下这个答案,了解如何编辑自动生成的代码。然后,在编辑代码时就可以做到这一点。不要忘了按
ctrl+ shift+ I来解决所有导入问题。我懒得写完全限定的名字。
jDesktopPane1 = new javax.swing.JDesktopPane() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
Run Code Online (Sandbox Code Playgroud)
如果需要图像,可以绘制图像
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);
}
};
Run Code Online (Sandbox Code Playgroud)
您还可以覆盖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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9040 次 |
最近记录: |