透明JPanel [英] Transparent JPanel

查看:144
本文介绍了透明JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个半透明的JPanel。我通过简单地使用颜色构造函数的RGBA值来完成它,但问题是当我使用事件处理时没有正确地进行操作。我的要求是一个半透明的Jpanel,当鼠标进入它时,此面板的边框变得可见,如果鼠标退出边框,则不可见。我通过以下代码完成了这个,但问题是它不能正常工作的透明背景(RGBA),但它适用于RGB颜色。

I want to create a semi-transparent JPanel. I've done it by simply using RGBA value of color constructor but problem is when i m using event handling is not woking properly. My requirement is a semi transparent Jpanel when mouse enters it border of this panel became visible and if mouse exit the border shoud not visible. I have done this by following code but problem is its not working properly for transparent backgroud (RGBA) but it working fine for RGB color.

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, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JPanel panel = new JPanel();

        panel.setBackground(new Color(0, 0, 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, 0));
        panel_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                panel_1.setBorder(new LineBorder(new Color(255, 255, 255), 5)); 
            }
            @Override
            public void mouseExited(MouseEvent e) {
                panel_1.setBorder(null);    
            }
        });
        panel_1.setBounds(84, 155, 215, 215);
        contentPane.add(panel_1);
    }
}


推荐答案

JPanel不支持半透明背景。处理此问题需要两个步骤:

JPanel does not support semi-transparent backgrounds. There are two steps needed to take care of this problem:


  • 首先,要想拥有任何正常运行的透明度,面板上的必须 setOpaque(false);否则你会有毛刺,因为假设一个不透明的面板完全覆盖其边界下面的东西。

  • First, to have any correctly-functioning transparency at all, you must setOpaque(false) on the panel; otherwise you will have glitches, because an opaque panel is assumed to completely cover what is underneath its bounds.

然而,当 opaque false ,面板也根本不绘制它的背景(!)所以你必须在paintComponent中绘制一个背景。

However, when opaque is false, the panel also does not draw its background at all (!) so you will have to draw a background in paintComponent.

这是一个免费替换课程,它将完成这两个步骤。

Here is a drop-in replacement class which will take care of both of these 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);
    }
}

我已经检查过它在您的程序中是否有效我将第一个面板创建更改为:

I’ve checked that it works in your program if I change the first panel creation to:

final JPanel panel = new TransparentPanel();

这篇关于透明JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆