首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >半透明JLabel没有正确显示背景

半透明JLabel没有正确显示背景
EN

Stack Overflow用户
提问于 2013-12-03 20:52:48
回答 2查看 2.9K关注 0票数 1

我有以下几句话:

代码语言:javascript
运行
复制
        label.setBackground(new java.awt.Color(0, 150, 0, 50));

我把它放在MouseAdapter中的一个MouseAdapter方法中。

基本上,我想让标签突出自己的半透明绿色时,我点击它。

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

我的问题是:

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

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

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

关于发生了什么有什么建议吗?我应该试着在这上面贴一个SSCCE吗?或者我错过了一个简单的答案。我还没有发布SSCCE的原因是我的代码很大,并且分布在多个文件中,所以我必须先把它修剪掉。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-03 21:16:25

有关可能的问题和几个解决方案,请参见透明背景

票数 2
EN

Stack Overflow用户

发布于 2013-12-03 21:38:44

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

通常,我会提供一个alpha值,然后应用到一个坚实的背景中,但是在这个例子中,我只是用你提供的任何背景颜色填充背景,所以除非你提供一个半透明的颜色,否则它将被填充一个坚实的颜色。

代码语言:javascript
运行
复制
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();
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20361432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档