gpt4 book ai didi

Java:透明的 JDialog 被重新漆成白色

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:09 32 4
gpt4 key购买 nike

我在 Windows 上的一个相当大的 Java 程序中遇到了一个非常奇怪的问题。我编写了一个小测试程序来重现该问题。

在 Windows 打开 UAC 提示覆盖后,自定义透明 JDialog 将被重新绘制为完全白色。

给出以下简单的测试类:

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class DialogTests extends JDialog {
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
EventQueue.invokeLater(() -> {
new DialogTests().setVisible(true);
});
}

public DialogTests() {
this.setAutoRequestFocus(false);
this.setUndecorated(true);
this.setAlwaysOnTop(true);
this.setFocusableWindowState(true);
this.setBackground(new Color(0,255,255,0));

JPanel contentPane = new JPanel();
contentPane.setBackground(new Color(0,0,0,200));
setContentPane(contentPane);
setBounds(200, 200, 500, 500);

JLabel label = new JLabel("this is just to see something!");
label.setForeground(new Color(255,0,0,255));
contentPane.add(label);

JButton button1 = new JButton("test button 1");
button1.setBackground(new Color(0,0,0,0));
contentPane.add(button1);

JButton button2 = new JButton("test button 2");
button2.setBackground(new Color(0,0,0,0));
contentPane.add(button2);
}

}

以下操作序列能够为我重现该问题:

  1. 启动程序。不要单击任何按钮或将鼠标移到对话框上!
  2. 强制显示 UAC 提示。例如,当您将 UAC 设置为最高安全级别时,禁用或启用网络适配器。确认提示。
  3. 点击“测试按钮 2”。对话框重新绘制为白色,只有两个按钮保持可见(因为它们是为了系统外观效果而重新绘制的)

如果您不想或无法重现该问题,这里有两个屏幕截图:

之前:

After starting

之后:

White paint bug

我想知道这个错误的解释,或者可能的解决方法。最好两者都:)

有关我正在使用的系统的一些详细信息:

  • Windows 7 x64
  • Java 8 u60
  • eclipse Mars 4.5.0(用于启动和调试)

非常感谢!

最佳答案

如果没有必要,请勿在颜色中使用不透明度。这会破坏有关不透明性的绘画契约,并可能导致绘画伪影。请参阅Background With Transparency对于可能导致的问题。

使用 255 时,组件是不透明的。当使用 0 时,组件是非透明的。所以只需使用:

component.setOpaque(true or false);

在框架或对话框上使用透明度时,您可以直接设置不透明度。尝试使用:

this.setBackground(new Color(0,255,255)); // play with this color
this.setOpacity(0.75f); // play with this opacity.
...
//contentPane.setBackground(new Color(0,0,0,200));
contentPane.setOpaque(false);

关于Java:透明的 JDialog 被重新漆成白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32529410/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com