Swing自定义淡入淡出式对话框,可自动关闭

可以自己调整停留时间,对话框颜色,透明度等等

package com.ui;

import com.constant.Fonts;
import sun.font.FontDesignMetrics;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 * @Author yzx
 * @Description 自定义淡入淡出式对话框
 * @Date 2021/1/5
 */
public class TipDialog extends JDialog {
    Dimension size;
    Font font = Fonts.NORMAL_MEDIUM;
    Color themeColor;

    // the auto closing option window constructor
    public TipDialog(Frame parentJFrame, String message) {
        super(parentJFrame);
        new Thread(() -> {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    close();
                }
            });
            // 设置主题色
            themeColor = Color.BLACK;
            FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
            int sWidth = metrics.stringWidth(message);
            int sHeight = metrics.getHeight();
            size = new Dimension(sWidth + 60, sHeight + 40);
            setSize(size);
            this.setUndecorated(true);
            // Dialog 背景透明
            setBackground(new Color(0,0,0,0));
            if (parentJFrame != null) {
                this.setLocationRelativeTo(parentJFrame);
            } else {
                this.setLocation(500, 300);
            }
            // the underground panel
            UndergroundPanel mainPanel = new UndergroundPanel();

            // the message label
            JLabel messageLabel = new JLabel(message, JLabel.CENTER);
            messageLabel.setForeground(Color.WHITE);
            messageLabel.setFont(font);
            mainPanel.setLayout(new BorderLayout());
            mainPanel.add(messageLabel, BorderLayout.CENTER);

            setLayout(new BorderLayout());
            add(mainPanel, BorderLayout.CENTER);

            // 渐隐效果显示
            float translucent = 0.0f;
            setVisible(true);
            while (translucent < 1) {
                setOpacity(translucent);
                translucent += 0.02f;
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 停留时间
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            close();
        }).start();
    }

    public void close() {
        // 渐隐效果
        float translucent = 1.0f;
        while (translucent > 0) {
            setOpacity(translucent);
            translucent -= 0.02f;
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        dispose();
    }


    private class UndergroundPanel extends JPanel {
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            int width = getWidth();
            int height = getHeight();
            // 避免锯齿
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
            g2d.setColor(themeColor);
            g2d.fillRoundRect(0, 0, width, height, 10, 10);
        }
    }

    public static void main(String[] args) {
        TipDialog test = new TipDialog(null, "测试文本");
    }
}

效果如下:
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值