windows 改变摇摆窗的底层背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2779035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing The Underlying Background Color Of A Swing Window
提问by dimo414
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.
正如此处所讨论的,在 Vista(以及我正在使用的 Windows 7)中调整 Swing 应用程序的大小时,您会在右/下角看到黑色背景,而 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.
使用其他应用程序(Windows 资源管理器(本机)、Firefox(C++?)和 Eclipse(Java))我注意到它们都有同样的问题 - 与上面链接中的人所说的相反 - 但他们通过具有灰色填充颜色,与 Swing 中出现的黑色相比,它在视觉上的不和谐性要小得多。
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.
我想知道是否有某种方法可以改变这一点,以便 Swing 的行为与这些其他应用程序一样?我尝试设置 JFrame 的背景颜色,但无济于事。
Additional InfoJonas 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.
附加信息Jonas 发现(请参阅下面的信息丰富的答案)这是 JFrames 的问题,而不是 AWT Frames - 也许这会帮助某人解决这个问题。
回答by Jonas
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.
我注意到了同样的问题。该颜色在 IE 中为灰色,在 Opera 中为黑色,在 Eclipse 中为灰色。它的接缝在 Swing 中更明显,因为它在重绘时接缝要慢一点,而且颜色如你所说,black。如果您使用左上角调整大小,则此问题更加明显。
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.
我编写了一个示例并试图了解此黑色的定义位置。甲的JFrame有许多层,所以设置的每个层上的不同的背景。
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.
但是这个例子没有帮助。也许颜色由超类设置为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.
我的理论是,由于 Swing 自己绘制,但使用本机 Window,那么在调整大小之前绘制本机背景,并在调整大小之后绘制 Swing 的背景。但是对于本机应用程序,背景是在调整大小之前绘制的。
UPDATE:I tried with a Framenow, and it's not having the same problem.The background seam to be painted before the resize.
更新:我现在尝试使用Frame,但没有出现同样的问题。在调整大小之前要绘制的背景接缝。
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);
}};
}
}
回答by Charles Goodwin
The frame is responsible for painting its background so you need to make sure you let it do its job.
框架负责绘制其背景,因此您需要确保让它发挥作用。
You demonstrate this by setting:
您可以通过设置来证明这一点:
System.setProperty("sun.awt.noerasebackground", "true");
This will cause the background to always be black on resize expansions. (So don't do it.)
这将导致背景在调整大小扩展时始终为黑色。(所以不要这样做。)
The following worked for me:
以下对我有用:
(AWT only) Set up double buffering using
createBufferStrategy(2)
- wrapped inaddNotify()
otherwise you'll run into exceptions during Frame creation(Step 1 is only necessary in AWT as Swing is double buffered by default.)
Always (important) call
super()
in yourFrame.paint()
implementationSet the background colour with
setBackground()
then the background should always be the same colour when expanding the frame
(仅限 AWT)使用
createBufferStrategy(2)
- 包裹设置双缓冲,addNotify()
否则您将在框架创建期间遇到异常(第 1 步仅在 AWT 中是必需的,因为 Swing 在默认情况下是双缓冲的。)
始终(重要)调用
super()
您的Frame.paint()
实现设置背景颜色,
setBackground()
然后扩展框架时背景应始终为相同颜色
Sample code:
示例代码:
class InnerFrame extends Frame {
public void addNotify() {
super.addNotify();
// Buffer
createBufferStrategy(2);
strategy = getBufferStrategy();
}
public void paint(Graphics g) {
super(g);
//...
}
//...
}
回答by Xorty
I also noticed this. For me this issue was solved with changing layout manager (I've used Free Form Layout before) and it worked pretty well (system color painting).
我也注意到了这一点。对我来说,这个问题是通过更改布局管理器解决的(我之前使用过自由格式布局)并且效果很好(系统颜色绘制)。
But eventually I switched back to FFL. Also some well known apps face this problem (f.e. SKYPE), but I actually don't mind it ...
但最终我又改回 FFL。还有一些著名的应用程序也面临这个问题(fe SKYPE),但我实际上并不介意......