jpanel透明度问题

发布于 2025-01-27 10:00:10 字数 328 浏览 7 评论 0原文

我有一个深灰色jpanel,上面有jlabel。我将设置为新的颜色(0,0,0,.5f)(tranparent)作为jlabel的背景,我使用按钮几次更改文本。问题是,每次更改文本时,先前的文本仍然保留在新文本后面。我将文本从“ 123456789”更改为“ 1234567”,“ 12345”和“ 123”。这是屏幕截图:

“

我如何摆脱此“影子”?

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

遇到 2025-02-03 10:00:10

我有一个黑灰色的jpanel,上面有一个jlabel。我设置了新的颜色(0,0,0,.5f)(tranparent)

秋千不支持透明背景。

Swing期望要么一个组件是:

  1. 不透明 - 这意味着该组件将首先用不透明的颜色重新粉刷整个背景,然后再进行自定义绘画或
  2. 完全透明 - 在这种情况下,秋千将首先绘制第一个不透明父母的背景,然后再进行。自定义绘画。

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.

孤芳又自赏 2025-02-03 10:00:10

此相关的示例也使jpanel半透明。

This related example also makes the JPanel translucent.

美人骨 2025-02-03 10:00:10

尝试一下,也许它将解决您的问题:
在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
}

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 和您的相关数据。
原文