Translucent JLabel not properly showing background

前端 未结 2 1725
天命终不由人
天命终不由人 2020-12-04 00:50

I have the following line:

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

I place this in a mouseReleased method within a

相关标签:
2条回答
  • 2020-12-04 01:27

    Swing only has a concept of opaque or transparent components, it does not, by itself, know how to deal with a component that is opaque, but has a translucent background color. As far as Swing is concerned, the component is opaque, so it doesn't paint what's underneath the component.

    Normally, I would supply a alpha value which I would then apply to a solid background, but in this example, I'm simply filling the background with what ever background color you supply, so unless you supply a translucent color, it will be filled with a solid color.

    enter image description here

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 01:48

    See Backgrounds With Transparency for the probable problem and a couple of solutions.

    0 讨论(0)
提交回复
热议问题