swing-组件Collapse折叠面板2

Collapse折叠面板2

在Collapse折叠面板中,已经实现了h5常见的折叠面板类型了.
还有一种类型的折叠面板,通过按钮控制折叠区域的显示于隐藏.
idea中run即采用这种结构

效果

在这里插入图片描述

思路分享

  • 整体的布局分为上下两部分,
  • 上半部分分为展示区域 和内容区域
  • 下半部分的按钮与内容区域相绑定
  • 根据内容区域的显示隐藏状态进行判定

代码


import cn.hutool.core.io.resource.ResourceUtil;
import com.formdev.flatlaf.FlatLightLaf;
import com.mynote.core.ui.ColorBuilder;
import com.mynote.core.util.FrameUtil;
import com.mynote.core.view.Row;
import com.mynote.example.demo.AbstractDefaultPanel;
import net.miginfocom.swing.MigLayout;
import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;

import javax.swing.*;
import javax.swing.border.Border;

/**
 * 测试仿idea折叠窗口
 */
public class DefaultDocPanel extends JPanel {

    private JPanel view;

    private JComponent wrapComp;
    private String docPath;

    private JButton docButton;
    private JScrollPane docView;

    private MultiSplitLayout multiSplitLayout;
    private Border border;


    public DefaultDocPanel(JComponent component, String docPath) {
        this.wrapComp = component;
        this.docPath = docPath;
        init();
        render();
        bindEvents();
    }

    /**
     * 创建doc视图
     *
     * @return
     */
    void createDocView() {
        String docContent = ResourceUtil.readUtf8Str("description/" + docPath);
        JLabel description = new JLabel(docContent);
        docView = new JScrollPane(description);
        docView.setBorder(border);
    }


    /**
     * 初始化成员对象
     */
    protected void init() {
        border = BorderFactory.createLineBorder(ColorBuilder.BG_BLANK_COLOR1, 2);
        view = new JPanel(new MigLayout("wrap 1"));
        createDocButton();
        createDocView();
        String defaultLayout = "(COLUMN " +
                "(LEAF name= demo weight=0.8)" +
                "(LEAF name=source weight=0.2)" +
                ")";
        multiSplitLayout = new MultiSplitLayout(MultiSplitLayout.parseModel(defaultLayout));
    }

    /**
     * render视图
     */
    protected void render() {
        JXMultiSplitPane splitPane = new JXMultiSplitPane();
        splitPane.setLayout(multiSplitLayout);
        splitPane.add(wrapComp, "demo");
        splitPane.add(docView, "source");
        view.add(splitPane, "w 100%,h 100%");
        view.add(docButton);
        super.setLayout(new MigLayout());
        super.add(view, "w 100%,h 100%");
    }

    /**
     * 绑定事件
     */
    protected void bindEvents() {
        docButton.addActionListener((e) -> {
            multiSplitLayout.displayNode("source", !docView.isVisible());
            updateDocButtonStyle(docView.isVisible());
        });
    }

    public void createDocButton() {
        if (docButton == null) {
            docButton = new JButton("doc");
            docButton.setFocusPainted(false);
            docButton.setBorderPainted(false);
            updateDocButtonStyle(true);
        }
    }

    /**
     * 更新button UI
     *
     * @param show
     */
    public void updateDocButtonStyle(boolean show) {
        if (show) {
            docButton.setBackground(ColorBuilder.BG_RED_COLOR1);
            docButton.setForeground(ColorBuilder.LABEL_WHITE_COLOR1);
            docButton.repaint();
        } else {
            docButton.setBackground(ColorBuilder.BG_BLANK_COLOR1);
            docButton.setForeground(ColorBuilder.LABEL_WHITE_COLOR1);
            docButton.repaint();
        }
    }


    public static void main(String[] args) {
        String html = "layout.html";
        String docContent = ResourceUtil.readUtf8Str("description/" + html);
        FlatLightLaf.install();
        FrameUtil.launchTest(new DefaultDocPanel(new JLabel(docContent), html));
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值