改变摇摆窗口的底层背景颜色 [英] Changing The Underlying Background Color Of A Swing Window

查看:108
本文介绍了改变摇摆窗口的底层背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处所述,调整Vista中的Swing应用程序(以及我正在使用的Windows 7),你会在右下角获得黑色背景,而Swing的重绘会赶上更改。

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 repaint catches up to the changes.

使用其他应用程序(Windows资源管理器(本机),Firefox(C ++?)和Eclipse(Java))我注意到他们都有同样的问题 - 与上面链接中的人说的相反 - 但是他们通过使用灰色填充颜色来最小化问题,这种颜色远远不如Swing中显示的黑色那么震撼。

Playing with other applications (Windows Explorer (Native), Firefox (C++?), and Eclipse (Java)) I notice that they all have this same problem - contrary to what the folks in the link above say - but they minimize the problem by having a grey fill color, which is far less visually jarring than the black that appears in Swing.

我想知道是否有某种方法可以改变这个所以Swing的行为与其他应用程序一样?我尝试设置JFrame的背景颜色,但无济于事。

I'm wondering if there's some way to change this so that Swing behaves like these other applications? I tried setting the background color of the JFrame, but to no avail.

其他信息
Jonas发现(请参阅他们的内容丰富的答案下面)这是JFrames的一个问题,但不是AWT框架 - 也许这会帮助有人解决这个问题。

Additional Info Jonas discovered (see their informative answer below) that this is an issue with JFrames, but not AWT Frames - maybe that will help someone figure this out.

推荐答案

我注意到了同样的问题。这种颜色在IE中是灰色的,在Opera中是黑色的,在Eclipse中它是灰色的。它在Swing中更明显地接缝,因为它在重新绘制时接缝要慢一点,颜色如你所说,黑色。如果您使用左上角来调整大小,则此问题更明显。

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.

我编写了一个示例,并尝试了解此黑色的定义位置。 JFrame有很多层,所以我设置了一个每层都有不同的背景。

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

我的理念是,由于Swing自己绘制,但使用原生窗口,然后是在调整大小之前绘制的本机背景,并且在调整大小之后绘制Swing的背景。但对于本机应用程序,在调整大小之前会绘制背景。

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

}

这篇关于改变摇摆窗口的底层背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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