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

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

问题描述

通过unsing netbeans ide,我在 JFrame 中创建了一个 JDesktopPane 。我无法改变jdesktopPane的颜色..我尽我所能。但当我在 JFrame JFrame .. JDesktopPane 时c>处于一些蓝色背景中。



请帮我改变 JDesktopPane的背景

解决方案

我假设你正在使用具有默认Nimbus外观的GUI Builder(因为你说你已经尝试了所有东西,我会假设你已经尝试过了) 的setBackground )。外观具有背景设置。但你可以选择它。


  1. 你可以只画背景。您想查看

      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(例外e){
    e.printStackTrace();
    }
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    new JDesktopPaneDemo();
    }
    });
    }

    静态类DesktopPainter实现了Painter< JComponent> {
    私人图片图片;

    public DesktopPainter(){
    try {
    image = ImageIO.read(new URL(http://www.hdbackgroundspoint.com/wp-content/uploads/2013 /09/hh.jpeg));
    } catch(IOException e){
    // TODO自动生成的catch块
    e.printStackTrace();
    }
    }

    @Override
    public void paint(Graphics2D g,JComponent对象,int width,int height){
    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);
        }
    }
    

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

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