swing-组件message消息提示

弹框可以对一些时间长的操作进行一种显著的提示,或者对于操作状态的警示, h5中的window.alert 也是开发中debug错误的一种方式, 所以弹框算是常用的一种组件.

swing实现弹框

在这里插入图片描述

  • JOptionPane 是最常用的类似于alert的弹框
  • JDialog 可以实现自定义弹框
  • GlassPane 也可以实现弹框 (JDialog 弹框会使主屏幕失去焦点,在一些场景上可能欠妥,GlassPane的提示可以很好的替代)

测试代码

package com.mynote.example.demo.tool;

import com.formdev.flatlaf.FlatLightLaf;
import com.mynote.core.comp.DialogLoading;
import com.mynote.core.ui.FontBuilder;
import com.mynote.core.ui.IconBuilder;
import com.mynote.core.util.CompBuilder;
import com.mynote.core.util.FrameUtil;
import com.mynote.example.demo.AbstractDefaultPanel;
import net.miginfocom.swing.MigLayout;

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

/**
 * 弹框测试
 */
public class LayerMsgTest extends AbstractDefaultPanel {

    private JButton alertMsg;
    private JButton layerMsg1;
    private JButton layerMsg2;


    @Override
    protected void init() {
        alertMsg = new JButton("alertMsg");
        layerMsg1 = new JButton("layerMsg1");
        layerMsg2 = new JButton("layerMsg2");
    }

    @Override
    protected void render() {
        view.setLayout(new MigLayout("wrap 1"));
        super.createViewRow(new JLabel("JOptionPane alert消息:"), alertMsg);
        super.createViewRow(new JLabel("JDialog 自定义消息:"), layerMsg1);
        super.createViewRow(new JLabel("glassPanel 自定义消息:"), layerMsg2);
        super.add(view);
    }


    @Override
    protected void bindEvents() {
        alertMsg.addActionListener((e) -> {
            JOptionPane.showMessageDialog(null, "请稍候", "提示", JOptionPane.YES_OPTION);
        });
        layerMsg1.addActionListener((e) -> {
            this.showDialogMsg(getFrame());
        });

        layerMsg2.addActionListener((e) -> {
            this.showGlassPaneMsg(getFrame());
        });


    }

    private JFrame getFrame() {
        Window win = SwingUtilities.getWindowAncestor(view);
        JFrame frame = (JFrame) win;
        if (frame == null) {
            throw new RuntimeException("获得不到JFrame窗口");
        }
        return frame;
    }

    /**
     * dialog msg
     *
     * @param frame
     */
    private void showDialogMsg(JFrame frame) {
        SwingUtilities.invokeLater(() -> {
            // 显示弹窗
            DialogLoading loading = new DialogLoading(frame);
            loading.showLoading("加载中...", IconBuilder.ICON_LOADING);
            // 2s关闭
            onceRun((event) -> {
                loading.hideLoading();
            });
        });
    }

    /**
     * glasspane msg
     *
     * @param frame
     */
    private void showGlassPaneMsg(JFrame frame) {
        JPanel bottomPanel = new JPanel(new MigLayout());
        bottomPanel.setOpaque(false);
        bottomPanel.add(createMessage(), "pos 0.5al 0.5al");
        SwingUtilities.invokeLater(() -> {
            //显示glasspane
            frame.setGlassPane(bottomPanel);
            frame.getGlassPane().setVisible(true);
            // 2s关闭
            onceRun((event) -> {
                frame.getGlassPane().setVisible(false);
            });
        });
    }

    /**
     * 创建消息面板
     *
     * @return
     */
    private JPanel createMessage() {
        JPanel panel = new JPanel(new MigLayout("w 300,h 240"));
        panel.setBackground(Color.WHITE);
        JLabel msg = new JLabel("处理完成");
        msg.setFont(FontBuilder.getLabelFont(20f));
        panel.add(new JLabel(IconBuilder.ICON_COMPLETE), "pos 0.5al 0.5al");
        panel.add(msg, "pos 0.5al 0.8al");
        return panel;
    }

    /**
     * 2s 定时运行
     *
     * @param actionListener
     */
    private void onceRun(ActionListener actionListener) {
        Timer timer = new Timer(2000, actionListener);
        timer.setRepeats(false);
        timer.start();
    }

    public static void main(String[] args) {
        FlatLightLaf.install();
        FrameUtil.launchTest(new LayerMsgTest());
    }
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值