Java 半透明JLabel未正确显示背景

Java 半透明JLabel未正确显示背景,java,swing,jlabel,Java,Swing,Jlabel,我有以下几行: label.setBackground(new java.awt.Color(0, 150, 0, 50)); 我把它放在MouseAdapter中的mouseerelease方法中 基本上,当我点击标签时,我想让它以半透明的绿色高亮显示 我在一个面板中有几个标签,都添加了这个MouseAdapter 我的问题是: -当我单击标签时,它显示半透明的绿色,但它显示的是另一个JLabel的背景,而不是我单击的那个 无论我点击哪个标签,它总是绘制同一标签的背景 -每当

我有以下几行:

        label.setBackground(new java.awt.Color(0, 150, 0, 50));
我把它放在MouseAdapter中的mouseerelease方法中

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

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

我的问题是:

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

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

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


有什么建议吗?我应该试着在这上面贴一张SSCCE吗?还是有一个简单的答案我不知道。我没有发布SSCCE的原因是我的代码很大,并且分布在多个文件中,所以我必须首先将其删除。

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

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

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();
        }
    }
}


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的支持,但是为什么
create()/dispose()
?只是良好的防御练习?@trashgod在这种情况下,没有真正的理由,我只是习惯性地这么做(并且在Netbeans中设置了模板来生成它;)@camickr Yea用于精美的图片;)@camickr Ohh slap:P-就个人而言,链接消失了(并不是说你的链接消失了,但我遇到了这个问题,我遇到了一些问题的答案,这很烦人;)@camickr说实话,在看MadProgrammer的解决方案之前,我用你的链接解决了我的问题。所以你的答案是对的。你们都是对的!!希望有两个复选标记?+1,但是为什么
create()/dispose()
?只是良好的防御练习?@trashgod在这种情况下,没有真正的理由,我只是习惯性地这么做(并且在Netbeans中设置了模板来生成它;)@camickr Yea用于精美的图片;)@camickr Ohh slap:P-就个人而言,链接消失了(并不是说你的链接消失了,但我遇到了这个问题,我遇到了一些问题的答案,这很烦人;)@camickr说实话,在看MadProgrammer的解决方案之前,我用你的链接解决了我的问题。所以你的答案是对的。你们都是对的!!希望有两个对勾?