Changing The Underlying Background Color Of A Swing Window

后端 未结 3 699
庸人自扰
庸人自扰 2020-12-30 06:16

As discussed here, when resizing a Swing application in Vista (and Windows 7, which is what I\'m using) you get a black background in the right/bottom corner while Swing\'s

3条回答
  •  悲哀的现实
    2020-12-30 06:26

    I have noticed the same problem. This color is gray at IE, in Opera it's black, and in Eclipse it's gray. It seam to be more visible in Swing, because it seam to be little slower at repainting and the color is as you said, black. This problem is more visible if you use the upper left corner to resize.

    I coded an example and tried to understand where this black color is defined. A JFrame has many layers, so I set a different background on every layer.

    import java.awt.Color;
    import javax.swing.JFrame;
    
    public class BFrame {
    
        public static void main(String[] args) {
            new JFrame() {{
                super.setBackground(Color.CYAN);
                this.getRootPane().setBackground(Color.BLUE);
                this.getLayeredPane().setBackground(Color.RED);
                this.getContentPane().setBackground(Color.YELLOW);
                this.setSize(400,340); 
                this.setVisible(true);
            }};
        }
    }
    

    But this example didn't help. And maybe the color is set by a superclass to Frame.

    java.lang.Object
      java.awt.Component
          java.awt.Container
              java.awt.Window
                  java.awt.Frame
    

    My teory is that, since Swing paints itself, but uses a native Window, then is the native background painted before the resize, and the background of Swing is painted after the resize. But for native applications, the background is painted before the resize.

    UPDATE: I tried with a Frame now, and it's not having the same problem. The background seam to be painted before the resize.

    import java.awt.Color;
    import java.awt.Frame;
    
    public class B2Frame extends Frame {
    
        public static void main(String[] args) {
            new Frame() {{
                setBackground(Color.YELLOW);
                setSize(400,340);
                setVisible(true);
            }};
        }
    
    }
    

提交回复
热议问题