免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2922 | 回复: 0
打印 上一主题 下一主题

Swing实现滑动折叠菜单源码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-27 16:04 |只看该作者 |倒序浏览
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JGroupPanel extends JPanel {
    /* 用来管理组的三个容器 */
    private JPanel pNorth = new JPanel() {
    };
    private JPanel pCenter = new JPanel();
    private JPanel pSouth = new JPanel();
    int[] openFlag = new int[3];
    /* 当前全部组的集合 */
    private ArrayList groupList = new ArrayList();
    /* 是否已禁止添加组件 */
    private boolean forbidFlag = false;
    String[][] str = new String[3][5];
    /* 当前激活的组 */
    private JGroupContainer activeGroup = null;
    transient ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JButton bttTitle = (JButton) e.getSource();
            expandGroup((JGroupContainer) bttTitle.getParent());
        }
    };
    public boolean hasCreateDefaultGroup = false;
    public JGroupPanel() {
        initComponents();
        createDefaultGroup();
    }
    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 BorderLayout());
        pSouth.setLayout(new GroupLayout());
        forbidFlag = true;
        for (int i = 0; i = 0; i--) {
            if (getGroupName(i).equals(name)) {
                expandGroup(i);
            }
        }
    }
    /**
     * 展开组
     *
     * @param index
     *            int 组的顺序号
     */
    public void expandGroup(int index) {
        expandGroup(getGroup(index));
    }
    /**
     * 展开组
     *
     * @param group
     *            JGroupContainer 组
     */
    protected void expandGroup(JGroupContainer group) {
        int num = 0;
        pNorth.removeAll();
        pCenter.removeAll();
        pSouth.removeAll();
        boolean hasAddCenter = false;
        for (int i = 0; i  i + 1) {
                    pNorth.add(c);
                } else {
                    pSouth.add(c);
                }
                openFlag = 0;
            }
        }
        if (activeGroup != null) {
            activeGroup.collapse();
        }
        for (int i = 0; i = 0; i--) {
            if (getGroupName(i).equals(name)) {
                collapseGroup(i);
            }
        }
    }
    /**
     * 收缩组
     *
     * @param index
     *            int 组的顺序号
     */
    public void collapseGroup(int index) {
        collapseGroup(getGroup(index));
    }
    /**
     * 收缩组
     *
     * @param group
     *            JGroupContainer 组
     */
    protected void collapseGroup(JGroupContainer group) {
        if (group == activeGroup) {
            activeGroup.collapse();
            activeGroup = null;
        }
    }
    /**
     * 添加组
     *
     * @param name
     *            String 组名
     */
    public void addGroup(String name) {
        this.insertGroup(getGroupCount(), name);
    }
    /**
     * 添加多个组
     *
     * @param names
     *            String[] 组名
     */
    public void addGroup(String names[]) {
        for (int i = 0; i  groupList.size()) {
            throw new ArrayIndexOutOfBoundsException("index:" + index
                    + " >count:" + groupList.size());
        }
        if (hasCreateDefaultGroup) {
            while (getGroupCount() > 0) {
                removeGroup(0);
            }
            hasCreateDefaultGroup = false;
        }
        int countNorth = pNorth.getComponentCount();
        int countCenter = pCenter.getComponentCount();
        int countSouth = pSouth.getComponentCount();
        JGroupContainer group;
        if (index = 0; i--) {
            if (getGroupName(i).equals(name)) {
                this.removeGroup(i);
            }
        }
    }
    /**
     * 设置组名
     *
     * @param index
     *            int 顺序号
     * @param name
     *            String 组名
     */
    public void setGroupName(int index, String name) {
        this.getGroup(index).setName(name);
    }
    /**
     * 取得组名
     *
     * @param groupIndex
     *            int 顺序号
     * @return String 组名
     */
    public String getGroupName(int groupIndex) {
        return getGroup(groupIndex).getName();
    }
    /**
     * 取得全部组名
     *
     * @return String[]
     */
    public String[] getGroupNames() {
        String sResult[] = new String[getGroupCount()];
        for (int i = 0; i
     * Title: OpenSwing
     *
     *
     * Description: 组面板布局管理器
     *
     *
     * Copyright: Copyright (c) 2004
     *
     *
     * Company:
     *
     *
     * @author SunKing
     * @version 1.0
     */
    class GroupLayout implements LayoutManager, java.io.Serializable {
        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
     * Title: OpenSwing
     *
     *
     * Description: 组
     *
     *
     * Copyright: Copyright (c) 2004
     *
     *
     * Company:
     *
     *
     * @author SunKing
     * @version 1.0
     */
    class JGroupContainer extends JPanel {
        private JButton bttGroupTitle = new JButton();
        private JPanel pMembers = new JPanel();
        // private JScrollPane sp;
        public JGroupContainer() {
            this("");
        }
        public JGroupContainer(String name) {
            this(name, UIManager.getColor("Desktop.background"));
        }
        /**
         * @param name
         *            String 组名
         * @param background
         *            Color 成员组件所在面板背景色
         */
        public JGroupContainer(String name, Color background) {
            bttGroupTitle.setText(name);
            bttGroupTitle.setFocusable(false);
            pMembers.setLayout(new GroupLayout(5, 5));
            this.setLayout(new BorderLayout());
            this.add(bttGroupTitle, BorderLayout.NORTH);
            // pMembers.setBackground(background);
            Color thumbColor = UIManager.getColor("ScrollBar.thumb");
            Color trackColor = UIManager.getColor("ScrollBar.track");
            Color trackHighlightColor = UIManager
                    .getColor("ScrollBar.trackHighlight");
            UIManager.put("ScrollBar.thumb", background);
            UIManager.put("ScrollBar.track", background);
            UIManager.put("ScrollBar.trackHighlight", background);
            /*
             * sp = new JScrollPane(pMembers); sp.setHorizontalScrollBarPolicy(
             * JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
             */
            this.add(pMembers, BorderLayout.CENTER);
            collapse();
            UIManager.put("ScrollBar.thumb", thumbColor);
            UIManager.put("ScrollBar.track", trackColor);
            UIManager.put("ScrollBar.trackHighlight", trackHighlightColor);
        }
        /**
         * 设置间距
         *
         * @param hgap
         *            int 横间距
         * @param vgap
         *            int 竖间距
         */
        public void setMemberGap(int hgap, int vgap) {
            pMembers.setLayout(new GroupLayout(hgap, vgap));
        }
        /**
         * 取得组的标题按钮
         *
         * @return JButton
         */
        public JButton getTitleButton() {
            return bttGroupTitle;
        }
        /**
         * 取得组的成员组件面板
         *
         * @return JPanel
         */
        public JPanel getMembersContainer() {
            return pMembers;
        }
        /**
         * 收缩组
         */
        public void collapse() {
            pMembers.setVisible(false);
            this.revalidate();
        }
        /**
         * 展开组
         */
        public void expand() {
            pMembers.setVisible(true);
            this.revalidate();
        }
        /**
         * 设置组名
         *
         * @param name
         *            String 组名
         */
        public void setName(String name) {
            bttGroupTitle.setText(name);
        }
        /**
         * 取得组名
         *
         * @return String
         */
        public String getName() {
            return bttGroupTitle.getText();
        }
        /**
         * 添加一个成员组件
         *
         * @param index
         *            int 顺序号
         * @param c
         *            Component 成员组件
         */
        public void addMember(int index, Component c) {
            pMembers.add(c, index);
            pMembers.doLayout();
        }
        /**
         * 删除一个成员组件
         *
         * @param index
         *            int 顺序号
         */
        public void removeMember(int index) {
            pMembers.remove(index);
            pMembers.doLayout();
        }
        /**
         * 取得一个成员组件
         *
         * @param index
         *            int 顺序号
         * @return Component 成员组件
         */
        public Component getMember(int index) {
            return pMembers.getComponent(index);
        }
        /**
         * 取得全部成员组件
         *
         * @return Component[] 成员组件
         */
        public Component[] getMembers() {
            Component coms[] = new Component[getMemberCount()];
            for (int i = 0; i < coms.length; i++) {
                coms = pMembers.getComponent(i);
            }
            return coms;
        }
        /**
         * 取得成员组件总数
         *
         * @return int 总数
         */
        public int getMemberCount() {
            return pMembers.getComponentCount();
        }
        /**
         * 重写的toString方法
         *
         * @return String
         */
        public String toString() {
            return getName();
        }
    }
    /**
     * /** 测试程序
     *
     * @param args
     *            String[]
     */
    public static void main(String[] args) {
        try {
            String windowsClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
            UIManager.setLookAndFeel((LookAndFeel) Class.forName(
                    windowsClassName).newInstance());
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JFrame frame = new JFrame("JGroupPanel Demo");
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(new JGroupPanel(), BorderLayout.CENTER);
        frame.setSize(150, 600);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width - frame.getSize().width - 10, 10);
        frame.setVisible(true);
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29346/showart_329518.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP