Swing开启遮罩层

 

package net.lucs.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import java.awt.FlowLayout;

public class Lesson extends JFrame {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					System.setProperty("awt.useSystemAAFontSettings", "on");
					System.setProperty("swing.aatext", "true");
					Lesson frame = new Lesson();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Lesson() {
		initialize();
	}

	private static final long UID = 1L;
	static final int Width = 400;
	static final int Height = 300;
	private JRootPane rootPane;
	private JLayeredPane layeredPane;
	private JButton btnNewButton_1;

	void initialize() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 833, 477);

		rootPane = new JRootPane();
		getContentPane().add(rootPane, BorderLayout.CENTER);

		layeredPane = new JLayeredPane();
		rootPane.getContentPane().add(layeredPane, BorderLayout.CENTER);

		btnNewButton_1 = new JButton("打开遮罩层");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rootPane.getGlassPane().show(true);
			}
		});
		layeredPane.setLayout(new BorderLayout(0, 0));
		layeredPane.add(btnNewButton_1, BorderLayout.NORTH);

		JPanel jPanel = new JPanel();
		// 半透明面板
		jPanel.setBackground(new Color(102, 0, 51, 180));
		JButton xxButton = new JButton("解锁");
		xxButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rootPane.getGlassPane().show(false);
			}
		});
		jPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		jPanel.add(xxButton);

		rootPane.setGlassPane(jPanel);

	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永恒天平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值