通过关闭 netbeans ide,我在 JFrame
中创建了一个 JDesktopPane
。而且我无法更改 jdesktopPane 的颜色。我已尽我所能。但是当我打开 JFrame
时,JFrame
中的 JDesktopPane
是蓝色背景。
请帮我更改JDesktopPane
的背景
最佳答案
我假设您正在使用具有默认 Nimbus 外观和感觉的 GUI Builder(因为您说您已经尝试了所有方法,我假设您已经尝试了 setBackground
) .外观设置了背景。但是您可以选择它。
你可以只画背景。你要看at this answer了解如何编辑自动生成的代码。然后,当您编辑代码时,您就可以做到这一点。之后不要忘记点击
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()); } };
如果你想要一个图像,你可以画一个图像
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 Defaults herepublic 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); } }
关于java - 如何更改使用 netbeans 中的工具创建的 jDesktopPane 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22864633/