如何更改使用 netbeans 中的工具创建的 jDesktopPane 的背景颜色 [英] How to change background color of jDesktopPane which is created usning tools in netbeans

查看:14
本文介绍了如何更改使用 netbeans 中的工具创建的 jDesktopPane 的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过取消 netbeans ide,我在 JFrame 中创建了一个 JDesktopPane.而且我无法更改 jdesktopPane 的颜色.. 我尽我所能.但是当我打开 JFrame .. JFrame 里面的 JDesktopPane 是一些蓝色背景.

请帮我更改JDesktopPane

的背景

解决方案

我将假设您正在使用具有默认 Nimbus 外观和感觉的 GUI Builder(因为您说您已经尝试了所有方法,我会假设您已经尝试过 setBackground).外观和感觉具有背景设置.但你有选择.

  1. 您可以只绘制背景.您想查看

    public static void main(String[] args) {尝试 {for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {if ("Nimbus".equals(laf.getName())) {UIManager.setLookAndFeel(laf.getClassName());UIManager.getLookAndFeelDefaults().put("DesktopPane[启用].backgroundPainter",新桌面画家());}}} 捕获(异常 e){e.printStackTrace();}SwingUtilities.invokeLater(new Runnable() {公共无效运行(){新的 JDesktopPaneDemo();}});}静态类 DesktopPainter 实现 Painter{私人形象形象;公共桌面画家(){尝试 {image = ImageIO.read(new URL("http://www.hdbackgroundspoint.com/wp-content/uploads/2013/09/hh.jpeg"));} catch (IOException e) {//TODO 自动生成的 catch 块e.printStackTrace();}}@覆盖公共无效油漆(Graphics2D g,JComponent 对象,整数宽度,整数高度){g.drawImage(image, 0, 0, width, height, null);}}

By unsing netbeans ide , I created a JDesktopPane inside the JFrame. and I cannot change the color of the jdesktopPane.. I tried all I can. But when I open the JFrame .. the JDesktopPane inside that JFrame is in some blue color background.

Please help me to change the background of JDesktopPane

解决方案

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

这篇关于如何更改使用 netbeans 中的工具创建的 jDesktopPane 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆