自定义组建面板

package com.gordon.groupPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class IsmGroupsPane extends JPanel {
    private static final long serialVersionUID = -7104845618141074287L;

    /* 用来管理组的三个容器 */
    private JPanel pNorth = new JPanel();
    private JPanel pCenter = new JPanel();
    private JPanel pSouth = new JPanel();

    /* 当前全部组的集合 */
    private ArrayList<IsmGroupPane> groupList = new ArrayList<IsmGroupPane>();
    private transient Handler handler = new Handler();

    private void initComponents() {
        this.setLayout(new BorderLayout());
        this.add(pNorth, BorderLayout.NORTH);
        this.add(pCenter, BorderLayout.CENTER);
        this.add(pSouth, BorderLayout.SOUTH);
        pNorth.setLayout(new GroupLayout());
        pCenter.setLayout(new GroupLayout());
        pSouth.setLayout(new GroupLayout());
    }

    public IsmGroupsPane(String groupNames[]) {
        initComponents();
        addGroup(groupNames);
    }

    /**
     * 展开组
     */
    public void expandGroup(String name) {
        for (int i = getGroupCount() - 1; i >= 0; i--) {
            if (getGroupName(i).equals(name)) {
                expandGroup(i);
            }
        }
    }

    public void addGroup(int index, Component contentPane) {
        getGroup(index).addContentPane(contentPane);
    }

    /**
     * 展开组
     */
    public void expandGroup(int index) {
        expandGroup(getGroup(index));
    }

    /**
     * 展开组
     */
    protected void expandGroup(IsmGroupPane group) {
        group.expand();
        pNorth.revalidate();
        pCenter.revalidate();
        pSouth.revalidate();
        this.revalidate();
    }

    /**
     * 添加组
     */
    public void addGroup(String name) {
        this.insertGroup(getGroupCount(), name);
    }

    /**
     * 添加多个组
     */
    public void addGroup(String names[]) {
        for (int i = 0; i < names.length; i++) {
            addGroup(names[i]);
        }
    }

    /**
     * 插入一个组
     */
    public void insertGroup(int index, String name, Color bg) {
        if (index < 0 || index > groupList.size()) {
            throw new ArrayIndexOutOfBoundsException("index:" + index
                    + " >count:" + groupList.size());
        }

        int countNorth = pNorth.getComponentCount();
        int countCenter = pCenter.getComponentCount();
        int countSouth = pSouth.getComponentCount();
        IsmGroupPane group;
        if (index <= countNorth) {
            group = insertGroup(pNorth, index, name, bg);
        } else if (index <= countNorth + countCenter) {
            group = insertGroup(pCenter, index - countNorth, name, bg);
        } else if (index <= countNorth + countCenter + countSouth) {
            group = insertGroup(pSouth, index - countNorth - countCenter, name,
                    bg);
        } else {
            group = insertGroup(pSouth, countSouth, name, bg);
        }
        group.getTitleButton().addActionListener(handler);
        groupList.add(index, group);

    }

    /**
     * 插入一个组
     */
    public void insertGroup(int index, String name) {
        insertGroup(index, name, UIManager.getColor("Desktop.background"));
    }

    /**
     * 插入一个组
     */
    private IsmGroupPane insertGroup(JPanel p, int index, String name, Color bg) {
        IsmGroupPane group = new IsmGroupPane(name);
        p.add(group);
        return group;
    }

    /**
     * 删除一个组
     */
    public void removeGroup(int index) {
        IsmGroupPane c = (IsmGroupPane) groupList.get(index);
        c.getParent().remove(c);
        c.getTitleButton().removeActionListener(handler);
    }

    /**
     * 删除一个组
     */
    public void removeGroup(String name) {
        for (int i = getGroupCount() - 1; i >= 0; i--) {
            if (getGroupName(i).equals(name)) {
                this.removeGroup(i);
            }
        }
    }

    /**
     * 设置组名
     */
    public void setGroupName(int index, String name) {
        this.getGroup(index).setName(name);
    }

    /**
     * 取得组名
     */
    public String getGroupName(int groupIndex) {
        return getGroup(groupIndex).getName();
    }

    /**
     * 取得全部组名
     */
    public String[] getGroupNames() {
        String sResult[] = new String[getGroupCount()];
        for (int i = 0; i < getGroupCount(); i++) {
            sResult[i] = getGroupName(i);
        }
        return sResult;
    }

    /**
     * 取得当前组的总数
     */
    public int getGroupCount() {
        return groupList.size();
    }

    /**
     * 取得组
     */
    protected IsmGroupPane getGroup(int index) {
        return groupList.get(index);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        JFrame frame = new JFrame();
        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("JGroupPanel Demo");
        frame.getContentPane().setLayout(new BorderLayout());
        IsmGroupsPane p = new IsmGroupsPane(new String[] { "aaaaa", "bbbbb" });
        p.addGroup(0, new JLabel("aaaaaaaaaa"));
        p.addGroup(1, new JLabel("bbbbb"));
        p.expandGroup("aaaaa");
        JScrollPane sp = new JScrollPane(p);
        frame.getContentPane().add(
                new JLabel("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                BorderLayout.NORTH);
        frame.getContentPane().add(sp, BorderLayout.CENTER);

        frame.setSize(150, 600);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width - frame.getSize().width - 10, 10);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

}

package com.gordon.groupPane;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JPanel;

public class IsmGroupPane extends JPanel {
    private static final long serialVersionUID = 1022271950394962755L;
    private JButton groupTitleButton = new JButton();
    private JPanel groupContentPane = new JPanel();
    private boolean hasExpanded;

    public IsmGroupPane(String name) {
        groupTitleButton.setText(name);
        groupTitleButton.setFocusable(false);
        groupContentPane.setLayout(new BorderLayout());

        this.setLayout(new BorderLayout());
        this.add(groupTitleButton, BorderLayout.NORTH);
        this.add(groupContentPane, BorderLayout.CENTER);
        this.collapse();
    }

    public IsmGroupPane(String name, Component contentPane) {
        this(name);
        addContentPane(contentPane);
    }

    public void addContentPane(Component contentPane) {
        groupContentPane.add(contentPane, BorderLayout.CENTER);
    }

    /**
     * 当前组是否已经展开
     *
     * @return
     */
    public boolean hasExpanded() {
        return hasExpanded;
    }

    /**
     * 收缩组
     */
    public void collapse() {
        hasExpanded = false;
        groupContentPane.setVisible(false);
        this.revalidate();
    }

    /**
     * 展开组
     */
    public void expand() {
        hasExpanded = true;
        groupContentPane.setVisible(true);
        this.revalidate();
    }

    /**
     * 取得组的标题按钮
     */
    public JButton getTitleButton() {
        return groupTitleButton;
    }

    /**
     * 取得组的成员组件面板
     */
    public JPanel getContentPane() {
        return groupContentPane;
    }

    /**
     * 取得组名
     */
    public String getName() {
        return groupTitleButton.getText();
    }

    /**
     * 取得一个成员组件
     */
    public Component getMember(int index) {
        return groupContentPane.getComponent(index);
    }

    /**
     * 取得全部成员组件
     */
    public Component[] getMembers() {
        Component coms[] = new Component[getMemberCount()];
        for (int i = 0; i < coms.length; i++) {
            coms[i] = groupContentPane.getComponent(i);
        }
        return coms;
    }

    /**
     * 取得成员组件总数
     */
    public int getMemberCount() {
        return groupContentPane.getComponentCount();
    }

    /**
     * 重写的toString方法
     */
    public String toString() {
        return getName();
    }

}


/**
 *
 */
package com.gordon.groupPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;

import javax.swing.JButton;

public class Handler extends MouseAdapter implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        JButton bttTitle = (JButton) e.getSource();
        IsmGroupPane group = (IsmGroupPane) bttTitle.getParent();
        if (group.hasExpanded()) {
            group.collapse();
        } else {
            group.expand();
        }
    }
}

package com.gordon.groupPane;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;

public class GroupLayout implements LayoutManager, java.io.Serializable {
    private static final long serialVersionUID = -4833058139929483074L;
    int vgap = 0;
    int hgap = 0;

    public GroupLayout() {
    }

    public GroupLayout(int hg, int vg) {
        this.hgap = hg;
        this.vgap = vg;
    }

    public void addLayoutComponent(String name, Component comp) {
    }

    public void removeLayoutComponent(Component comp) {
    }

    public Dimension preferredLayoutSize(Container parent) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            int ncomponents = parent.getComponentCount();
            int w = 0;
            int h = 0;
            for (int i = 0; i < ncomponents; i++) {
                Component comp = parent.getComponent(i);
                Dimension d = comp.getPreferredSize();
                if (w < d.width) {
                    w = d.width;
                }
                h += d.height + vgap;
            }
            return new Dimension(insets.left + insets.right + w + 2 * hgap,
                    insets.top + insets.bottom + h + 2 * vgap);
        }
    }

    public Dimension minimumLayoutSize(Container parent) {
        return preferredLayoutSize(parent);
    }

    public void layoutContainer(Container parent) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            int ncomponents = parent.getComponentCount();
            if (ncomponents == 0) {
                return;
            }
            int y = insets.top + vgap;
            for (int c = 0; c < ncomponents; c++) {
                int h = parent.getComponent(c).getPreferredSize().height;
                parent.getComponent(c).setBounds(
                        insets.left + hgap,
                        y,
                        parent.getWidth() - insets.left - insets.right - 2
                                * hgap, h);
                y += h + vgap;
            }
        }
    }

    // public String toString() {
    // return groupPanel.getClass().getName();
    // }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值