其他分享
首页 > 其他分享> > swing-组件测试实现模态框

swing-组件测试实现模态框

作者:互联网

效果如图

在这里插入图片描述

模态框是web开发中常用的一种组件,可以用作页面的内容的拓展\交互\提示等等, swing常见的组件不包含模态框, 窗口内组件jdialog或者jwindow的弹窗更像iframe那种二级窗口.

思路

demo测试代码

dependencies:
com.mynote.core.**
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.formdev.flatlaf.FlatLightLaf;
import com.mynote.core.ui.ColorBuilder;
import com.mynote.core.util.FrameUtil;
import net.miginfocom.swing.MigLayout;
import org.jdesktop.swingx.JXPanel;

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

/**
 * 模态框效果
 */
public class ModalPanelTest extends AbstractDefaultPanel {

    private JButton showModal;

    private JTextArea textArea;

    private JLayeredPane layeredPane;

    private JPanel content;

    private JXPanel modalMask;

    private JButton closeModal;

    private JXPanel modalContent;


    /**
     * 初始化成员对象
     */
    @Override
    protected void init() {
        layeredPane = new JLayeredPane();
        createTextArea();
        createModalButton();
        createModalMask();
        createModalContent();
        content = new JPanel();
        content.setLayout(new MigLayout("wrap 1"));
        content.add(showModal, "align right");
        content.add(new JScrollPane(textArea), "w 100%,h 100%");
    }

    /**
     * 创建按钮
     */
    void createModalButton() {
        showModal = new JButton("showModal");
        showModal.setBackground(ColorBuilder.BG_RED_COLOR1);
        showModal.setForeground(ColorBuilder.LABEL_WHITE_COLOR1);
        closeModal = new JButton("closeModal");
    }

    /**
     * 创建文本域
     */
    void createTextArea() {
        textArea = new JTextArea(RandomUtil.randomString(3000));
        textArea.setLineWrap(true);
    }

    /**
     * 创建遮罩层
     */
    void createModalMask() {
        modalMask = new JXPanel();
        modalMask.setLayout(new MigLayout("w 100%,h 100%"));
        modalMask.setAlpha(0.5F);
        modalMask.setOpaque(false);
        modalMask.setBackground(ColorBuilder.LABEL_GRAY_COLOR1);
        modalMask.setVisible(false);
    }


    /**
     * 创建modal面板
     */
    void createModalContent() {
        modalContent = new JXPanel();
        modalContent.setLayout(new MigLayout("wrap 1,center"));
        modalContent.add(new JLabel(DateUtil.now()),"gaptop 50");
        modalContent.add(new JTextField(), "w 200");
        modalContent.setBackground(ColorBuilder.LABEL_WHITE_COLOR1);
        modalContent.add(closeModal, "pos 1al 1al");
        modalContent.setVisible(false);
    }


    /**
     * render视图
     */
    @Override
    protected void render() {
        view.add(layeredPane, "w 100%,h 100%");
        // content 默认显示
        layeredPane.setLayout(new MigLayout("wrap 1,w 100%,h 100%"));
        layeredPane.setLayer(content, JLayeredPane.DEFAULT_LAYER);
        layeredPane.add(content, "w 100%,h 100%");

        // modalmask 绝对位置显示遮罩
        layeredPane.setLayer(modalMask, JLayeredPane.PALETTE_LAYER);
        layeredPane.add(modalMask, "pos 0.5al 0.5al,w 100%,h 100%");

        // modalcontent 绝对位置显示模态框
        layeredPane.setLayer(modalContent, JLayeredPane.MODAL_LAYER);
        layeredPane.add(modalContent, "pos 0.5al 0.5al,w 70%,h 70%");

        super.add(view, "w 100%,h 100%");
    }


    /**
     * 绑定事件
     */
    @Override
    protected void bindEvents() {
        showModal.addActionListener((e) -> {
            modalMask.setVisible(true);
            modalContent.setVisible(true);
            setContentEnables(false);
        });

        closeModal.addActionListener((e) -> {
            modalMask.setVisible(false);
            modalContent.setVisible(false);
            setContentEnables(true);
        });
    }

    /**
     * 是否禁用content面板
     *
     * @param enable
     */
    void setContentEnables(boolean enable) {
        Component[] components = content.getComponents();
        for (Component comp : components) {
            comp.setEnabled(enable);
            System.out.println("当前节点:" + comp);
            // jscrollpanel 需要找到viewport 特殊处理
            if (comp instanceof JScrollPane) {
                JViewport scrollView = (JViewport) ((JScrollPane) comp).getComponent(0);
                Component[] scrollViewComps = scrollView.getComponents();
                for (Component scrollViewComp : scrollViewComps) {
                    System.out.println("scroll节点:" + scrollViewComp);
                    scrollViewComp.setEnabled(enable);
                }
            }
        }
    }


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

标签:模态,100%,layeredPane,modalMask,content,modalContent,swing,组件,new
来源: https://blog.csdn.net/x308561498/article/details/122817137