【问题标题】:Translucent JLabel not properly showing background半透明 JLabel 未正确显示背景
【发布时间】:2026-02-10 15:40:01
【问题描述】:

我有以下行:

        label.setBackground(new java.awt.Color(0, 150, 0, 50));

我将它放在 MouseAdapter 内的 mouseReleased 方法中。

基本上,当我点击它时,我想让标签以半透明的绿色突出显示。

我在一个面板中有几个标签,所有标签都添加了这个 MouseAdapter。

我的问题是这样的:

-当我点击标签时,它显示半透明的绿色,但它显示的是另一个JLabel的背景,而不是我点击的那个。

无论我点击哪个标签,它总是绘制同一个标签的背景。

-每当我单击标签时,它都会重复相同的背景。 - 奇怪的是,每次我点击一个 JLabel 时,绿色的不透明度似乎会增加,好像每次我点击一个新的 JLabel 时它都会在自身上涂上半透明的绿色。

关于发生了什么的任何提示?我应该尝试在此发布 SSCCE 吗?还是有一个我缺少的简单答案。我还没有发布SSCCE的原因是我的代码很大并且分布在多个文件中,所以我必须先把它修剪掉。

【问题讨论】:

  • 这段代码对我们有很大帮助。事实上,您可能会在重新创建一个独立的示例时自己发现问题。
  • 您至少可以发布最后分配label 变量的代码。这是最有可能与为什么选择错误标签相关的代码!

标签: java swing jlabel


【解决方案1】:

请参阅Backgrounds With Transparency 了解可能的问题和一些解决方案。

【讨论】:

  • 很棒的文章。让我明白我的问题。谢谢
【解决方案2】:

Swing 只有不透明或透明组件的概念,它本身并不知道如何处理不透明但具有半透明背景颜色的组件。就 Swing 而言,组件是不透明的,因此它不会绘制组件下方的内容。

通常,我会提供一个 alpha 值,然后将其应用于纯色背景,但在此示例中,我只是用您提供的任何背景颜色填充背景,因此除非您提供半透明颜色,它将被填充为纯色。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTranslucentLabel {

    public static void main(String[] args) {
        new TestTranslucentLabel();
    }

    public TestTranslucentLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                try {
                    TranslucentLabel label = new TranslucentLabel("This is a translucent label");
                    label.setBackground(new Color(255, 0, 0, 128));
                    label.setForeground(Color.WHITE);

                    JLabel background = new JLabel();
                    background.setIcon(new ImageIcon(ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Rampage_Small.png"))));
                    background.setLayout(new GridBagLayout());
                    background.add(label);

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(background);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

    public class TranslucentLabel extends JLabel {

        public TranslucentLabel(String text, Icon icon, int horizontalAlignment) {
            super(text, icon, horizontalAlignment);
        }

        public TranslucentLabel(String text, int horizontalAlignment) {
            super(text, horizontalAlignment);
        }

        public TranslucentLabel(String text) {
            super(text);
        }

        public TranslucentLabel(Icon image, int horizontalAlignment) {
            super(image, horizontalAlignment);
        }

        public TranslucentLabel(Icon image) {
            super(image);
        }

        public TranslucentLabel() {
            super();
        }

        @Override
        public boolean isOpaque() {
            return false;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            super.paintComponent(g2d);
            g2d.dispose();
        }
    }
}

【讨论】:

  • +1 表示sscce,但为什么是create()/dispose()?只是良好的防守练习?
  • @trashgod 在这种情况下,没有真正的原因,我只是习惯性地这样做(并在 Netbeans 中设置了模板来生成它;))
  • @camickr 是的,对于精美的图片;)
  • @camickr Ohh slap :P - 就个人而言,链接消失了(不是说你的,但我遇到了这个问题,有几个我遇到的问题的答案,这很烦人;))跨度>
  • @camickr 老实说,在查看 MadProgrammer 的解决方案之前,我通过使用您的链接解决了我的问题。所以你的回答是对的。你俩都对!!希望有两个复选标记?