Java – transparent JPanel

I want to create a translucent JPanel I do this by simply using the RGBA value of the color constructor, but the problem is when I wake up incorrectly using event handling My requirement is a translucent JPanel. When the mouse enters its border, the panel becomes visible. If the mouse exits, the border is not visible I've done this through the following code, but the problem is that it doesn't work with a transparent background (RGBA), but it can work with RGB colors

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class MDCW extends JFrame {

      private JPanel contentPane;

     /**
     * Launch the application.
     */
     public static void main(String[] args) {
        EventQueue.invokelater(new Runnable() {
            public void run() {
                try {
                    MDCW frame = new MDCW();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MDCW() {
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100,100,1013,551);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(0,139,139));
        contentPane.setBorder(new EmptyBorder(5,5,5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JPanel panel = new JPanel();

        panel.setBackground(new Color(0,50));
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                panel.setBorder(new LineBorder(new Color(255,255,255),5));   
            }
            @Override
            public void mouseExited(MouseEvent e) {
                panel.setBorder(null);  
            }
        });
        panel.setBounds(360,155,215,215);
        contentPane.add(panel);

        final JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(0,0));
        panel_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                panel_1.setBorder(new LineBorder(new Color(255,5)); 
            }
            @Override
            public void mouseExited(MouseEvent e) {
                panel_1.setBorder(null);    
            }
        });
        panel_1.setBounds(84,215);
        contentPane.add(panel_1);
    }
}

Solution

JPanel does not support translucent backgrounds Two steps are needed to deal with this problem:

>First, to make any transparency work properly, you must set opacity (false) on the panel; Otherwise, you will have burrs, because it is assumed that an opaque panel completely covers its boundary. > However, when opaque is false, the panel will not draw its background (!), So you must draw the background in paintcomponent

This is a drop - down replacement class that handles these two steps

private class TransparentPanel extends JPanel {
    {
        setOpaque(false);
    }
    public void paintComponent(Graphics g) {
        g.setColor(getBackground());
        Rectangle r = g.getClipBounds();
        g.fillRect(r.x,r.y,r.width,r.height);
        super.paintComponent(g);
    }
}

If I change the first panel creation to:

final JPanel panel = new TransparentPanel();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>