How to change background color of jDesktopPane which is created usning tools in netbeans

余生长醉 提交于 2019-11-29 07:49:09
Paul Samsotha

I'm going to assume you're using GUI Builder with the default Nimbus look and feel (because you said you've tried everything, and I'll assume you've tried setBackground). The look and feel has the background set. But you have options around it.

  1. You can just paint the background. You want to look at this answer for how to edit the auto-generated code. Then you can just to this, when you edit the code. Don't forget to hit
    ctrl+shift+I afterwards, to resolve all imports. I'm too lazy to write fully qualified names.

    jDesktopPane1 = new javax.swing.JDesktopPane() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    };
    

  2. If you want an image, you can paint an image

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

  3. You could also override the Nimbus default DesktopPane[Enabled].backgroundPainter. See Nimbus Defaults here

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!