JPanel透明度问题

发布于 2024-10-10 20:33:32 字数 314 浏览 3 评论 0原文

我有一个深灰色的 JPanel ,上面有一个 JLabel 。我将 new Color(0, 0, 0, .5f) (透明)设置为 JLabel 的背景,并使用按钮多次更改文本。问题是,每次更改文本时,之前的文本仍然保留在新文本后面。我将文本从“123456789”更改为“1234567”、“12345”和“123”。这是屏幕截图:

alt text

如何摆脱这个“阴影”?

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent) as the background of the JLabel and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:

alt text

How do I get rid of this "shadow"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

且行且努力 2024-10-17 20:33:32

我有一个深灰色的 JPanel,上面有一个 JLabel。我设置了 new Color(0, 0, 0, .5f) (透明)

Swing 不支持透明背景。

Swing 期望组件是:

  1. 不透明 - 这意味着组件在进行自定义绘制之前将首先使用不透明颜色重新绘制整个背景,或者
  2. 完全透明 - 在这种情况下,Swing 将在执行自定义绘制之前首先绘制第一个不透明父组件的背景定制绘画。

setOpaque(...) 方法用于控制组件的不透明属性。

在任何一种情况下,这都可以确保消除任何绘画伪影,并且可以正确完成自定义绘画。

如果您想使用透明度,那么您需要自己进行自定义绘画以确保背景被清除。

面板的自定义绘制如下:

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false); // background of parent will be painted first

每个使用透明度的组件都需要类似的代码。

或者,您可以查看具有透明度的背景进行自定义可以在任何为您完成上述工作的组件上使用的类。

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent)

Swing does not support transparent backgrounds.

Swing expects a component to be either:

  1. opaque - which implies the component will repaint the entire background with an opaque color first before doing custom painting, or
  2. fully transparent - in which case Swing will first paint the background of the first opaque parent component before doing custom painting.

The setOpaque(...) method is used to control the opaque property of a component.

In either case this makes sure any painting artifacts are removed and custom painting can be done properly.

If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.

The custom painting for the panel would be:

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false); // background of parent will be painted first

Similar code would be required for every component that uses transparency.

Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.

迷爱 2024-10-17 20:33:32

此相关示例也使JPanel半透明。

This related example also makes the JPanel translucent.

微凉徒眸意 2024-10-17 20:33:32

试试这个,也许它能解决你的问题:
行动中表现..

public void actionPerformed(ActionEvent e) {
    final JLabel tmpLabel = new JLabel(value[++i]); //change text
    label.setFont(new Font("Times New Roman", 1, 36));
    label.setForeground(new Color(255, 255, 255));
    label.setBackground(new Color(0, 0, 0, .5f));
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setOpaque(true);
    label.setBounds(10, 10, 270, 70);
    label = tmpLabel; //replace the entire label with a new label
}

try this, maybe it will solve your problem:
In actionPeroformed..

public void actionPerformed(ActionEvent e) {
    final JLabel tmpLabel = new JLabel(value[++i]); //change text
    label.setFont(new Font("Times New Roman", 1, 36));
    label.setForeground(new Color(255, 255, 255));
    label.setBackground(new Color(0, 0, 0, .5f));
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setOpaque(true);
    label.setBounds(10, 10, 270, 70);
    label = tmpLabel; //replace the entire label with a new label
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文