JAVA实现卷帘式菜单

  1. package windows.best_demo;  
  2.   
  3.   
  4. import java.awt.*;     
  5. import javax.swing.*;     
  6. import java.util.*;     
  7. import java.awt.event.*;     
  8.      
  9. /**   
  10.  * 

    Title:OpenSwing 

      
     
  11.  * 

    Description: JGroupPanel 组群面板    

  12.  *  类似QQ界面的组群管理面板   
  13.  * 

      
     
  14.  * 履历:    
  15.  *     2004/07/24   由SunKing作成    
  16.  * 

    Copyright: Copyright (c) 2004

      
     
  17.  * 

    Company: 

      
     
  18.  * @author Sunking   
  19.  * @version 1.0   
  20.  */     
  21. public class JGroupPanel     
  22.     extends JPanel {     
  23.     /*用来管理组的三个容器*/     
  24.     private JPanel pNorth = new JPanel() {     
  25.     };     
  26.     private JPanel pCenter = new JPanel();     
  27.     private JPanel pSouth = new JPanel();     
  28.      
  29.     /*当前全部组的集合*/     
  30.     private ArrayList groupList = new ArrayList();     
  31.      
  32.     /*是否已禁止添加组件*/     
  33.     private boolean forbidFlag = false;     
  34.      
  35.     /*当前激活的组*/     
  36.     private JGroupContainer activeGroup = null;     
  37.     transient ActionListener al = new ActionListener() {     
  38.         public void actionPerformed(ActionEvent e) {     
  39.             JButton bttTitle = (JButton) e.getSource();     
  40.             expandGroup( (JGroupContainer) bttTitle.getParent());     
  41.         }     
  42.     };     
  43.      
  44.     private boolean hasCreateDefaultGroup = false;     
  45.     public JGroupPanel() {     
  46.         initComponents();     
  47.         createDefaultGroup();     
  48.     }     
  49.     private void initComponents(){     
  50.         this.setLayout(new BorderLayout());     
  51.         this.add(pNorth, BorderLayout.NORTH);     
  52.         this.add(pCenter, BorderLayout.CENTER);     
  53.         this.add(pSouth, BorderLayout.SOUTH);     
  54.         pNorth.setLayout(new GroupLayout());     
  55.         pCenter.setLayout(new BorderLayout());     
  56.         pSouth.setLayout(new GroupLayout());     
  57.         forbidFlag = true;     
  58.     }     
  59.     private void createDefaultGroup(){     
  60.         //Default Group      
  61.         Color bg[] = {     
  62.             Color.black, Color.red, Color.orange, Color.yellow, Color.green,     
  63.             Color.cyan, Color.blue, Color.white};     
  64.         for (int i = 1; i <= bg.length; i++) {     
  65.             insertGroup(i - 1"Group " + i, bg[i - 1]);     
  66.             Color mc = new Color(255 - bg[i - 1].getRed(),     
  67.                                  255 - bg[i - 1].getGreen(),     
  68.                                  255 - bg[i - 1].getBlue());     
  69.             for (int j = 1; j <= 5; j++) {     
  70.                 JButton bttMember = new JButton("Member " + j + " of " + i);     
  71.                 addMember(i - 1, bttMember);     
  72.                 bttMember.setPreferredSize(new Dimension(140));     
  73.                 bttMember.setOpaque(false);     
  74.                 bttMember.setForeground(mc);     
  75.             }     
  76.             getGroup(i - 1).setMemberGap(205);     
  77.             getGroup(i - 1).getTitleButton().setForeground(bg[i - 1]);     
  78.         }     
  79.         expandGroup(0);     
  80.         hasCreateDefaultGroup = true;     
  81.     }     
  82.      
  83.     /**   
  84.      * @param groupNames String[] 预设组名   
  85.      */     
  86.     public JGroupPanel(String groupNames[]) {     
  87.         initComponents();     
  88.         addGroup(groupNames);     
  89.     }     
  90.      
  91.     /**   
  92.      * 展开组   
  93.      * @param name String 组名   
  94.      */     
  95.     public void expandGroup(String name) {     
  96.         for (int i = getGroupCount() - 1; i >= 0; i--) {     
  97.             if (getGroupName(i).equals(name)) {     
  98.                 expandGroup(i);     
  99.             }     
  100.         }     
  101.     }     
  102.      
  103.     /**   
  104.      * 展开组   
  105.      * @param index int 组的顺序号   
  106.      */     
  107.     public void expandGroup(int index) {     
  108.         expandGroup(getGroup(index));     
  109.     }     
  110.      
  111.     /**   
  112.      * 展开组   
  113.      * @param group JGroupContainer 组   
  114.      */     
  115.     protected void expandGroup(JGroupContainer group) {     
  116.         pNorth.removeAll();     
  117.         pCenter.removeAll();     
  118.         pSouth.removeAll();     
  119.         boolean hasAddCenter = false;     
  120.         for (int i = 0; i < groupList.size(); i++) {     
  121.             Component c = (Component) groupList.get(i);     
  122.             if (hasAddCenter) {     
  123.                 pSouth.add(c);     
  124.             }     
  125.             else if (c == group) {     
  126.                 pCenter.add(c, BorderLayout.CENTER);     
  127.                 hasAddCenter = true;     
  128.             }     
  129.             else {     
  130.                 pNorth.add(c);     
  131.             }     
  132.         }     
  133.         if (activeGroup != null) {     
  134.             activeGroup.collapse();     
  135.         }     
  136.         activeGroup = group;     
  137.         activeGroup.expand();     
  138.         pNorth.doLayout();     
  139.         pCenter.doLayout();     
  140.         pSouth.doLayout();     
  141.         doLayout();     
  142.     }     
  143.      
  144.     /**   
  145.      * 收缩组   
  146.      * @param name String 组名   
  147.      */     
  148.     public void collapseGroup(String name) {     
  149.         for (int i = getGroupCount() - 1; i >= 0; i--) {     
  150.             if (getGroupName(i).equals(name)) {     
  151.                 collapseGroup(i);     
  152.             }     
  153.         }     
  154.     }     
  155.      
  156.     /**   
  157.      * 收缩组   
  158.      * @param index int 组的顺序号   
  159.      */     
  160.     public void collapseGroup(int index) {     
  161.         collapseGroup(getGroup(index));     
  162.     }     
  163.      
  164.     /**   
  165.      * 收缩组   
  166.      * @param group JGroupContainer 组   
  167.      */     
  168.     protected void collapseGroup(JGroupContainer group) {     
  169.         if (group == activeGroup) {     
  170.             activeGroup.collapse();     
  171.             activeGroup = null;     
  172.         }     
  173.     }     
  174.      
  175.     /**   
  176.      * 添加组   
  177.      * @param name String 组名   
  178.      */     
  179.     public void addGroup(String name) {     
  180.         this.insertGroup(getGroupCount(), name);     
  181.     }     
  182.      
  183.     /**   
  184.      * 添加多个组   
  185.      * @param names String[] 组名   
  186.      */     
  187.     public void addGroup(String names[]) {     
  188.         for (int i = 0; i < names.length; i++) {     
  189.             addGroup(names[i]);     
  190.         }     
  191.     }     
  192.      
  193.     /**   
  194.      * 插入一个组   
  195.      * @param index int 顺序号   
  196.      * @param name String 组名   
  197.      * @param bg Color 背景色   
  198.      */     
  199.     public void insertGroup(int index, String name, Color bg) {     
  200.         if (index < 0 || index > groupList.size()) {     
  201.             throw new ArrayIndexOutOfBoundsException("index:" + index +     
  202.                 " >count:" + groupList.size());     
  203.         }     
  204.         if(hasCreateDefaultGroup){     
  205.             while(getGroupCount()>0){     
  206.                 removeGroup(0);     
  207.             }     
  208.             hasCreateDefaultGroup = false;     
  209.         }     
  210.         int countNorth = pNorth.getComponentCount();     
  211.         int countCenter = pCenter.getComponentCount();     
  212.         int countSouth = pSouth.getComponentCount();     
  213.         JGroupContainer group;     
  214.         if (index <= countNorth) {     
  215.             group = insertGroup(pNorth, index, name, bg);     
  216.         }     
  217.         else if (index <= countNorth + countCenter) {     
  218.             group = insertGroup(pCenter, index - countNorth, name, bg);     
  219.         }     
  220.         else if (index <= countNorth + countCenter + countSouth) {     
  221.             group = insertGroup(pSouth, index - countNorth - countCenter, name,     
  222.                                 bg);     
  223.         }     
  224.         else {     
  225.             group = insertGroup(pSouth, countSouth, name, bg);     
  226.         }     
  227.         group.getTitleButton().addActionListener(al);     
  228.         groupList.add(index, group);     
  229.      
  230.     }     
  231.      
  232.     /**   
  233.      * 插入一个组   
  234.      * @param index int 顺序号   
  235.      * @param name String 组名   
  236.      */     
  237.     public void insertGroup(int index, String name) {     
  238.         insertGroup(index, name, UIManager.getColor("Desktop.background"));     
  239.     }     
  240.      
  241.     /**   
  242.      * 插入一个组   
  243.      * @param p JPanel 目标面板   
  244.      * @param index int 顺序号   
  245.      * @param name String 组名   
  246.    
  247.    
  248.          /**   
  249.       * 插入一个组   
  250.       * @param p JPanel 目标面板   
  251.       * @param index int 顺序号   
  252.       * @param name String 组名   
  253.       * @return JGroupContainer   
  254.       */     
  255.      private JGroupContainer insertGroup(JPanel p, int index, String name,     
  256.                                          Color bg) {     
  257.          JGroupContainer group = new JGroupContainer(name, bg);     
  258.          p.add(group);     
  259.          return group;     
  260.      }     
  261.      
  262.     /**   
  263.      * 删除一个组   
  264.      * @param index int 顺序号   
  265.      */     
  266.     public void removeGroup(int index) {     
  267.         JGroupContainer c = (JGroupContainer) groupList.get(index);     
  268.         c.getParent().remove(c);     
  269.         c.getTitleButton().removeActionListener(al);     
  270.     }     
  271.      
  272.     /**   
  273.      * 删除一个组   
  274.      * @param name String 组名   
  275.      */     
  276.     public void removeGroup(String name) {     
  277.         for (int i = getGroupCount() - 1; i >= 0; i--) {     
  278.             if (getGroupName(i).equals(name)) {     
  279.                 this.removeGroup(i);     
  280.             }     
  281.         }     
  282.     }     
  283.      
  284.     /**   
  285.      * 设置组名   
  286.      * @param index int 顺序号   
  287.      * @param name String 组名   
  288.      */     
  289.     public void setGroupName(int index, String name) {     
  290.         this.getGroup(index).setName(name);     
  291.     }     
  292.      
  293.     /**   
  294.      * 取得组名   
  295.      * @param groupIndex int 顺序号   
  296.      * @return String 组名   
  297.      */     
  298.     public String getGroupName(int groupIndex) {     
  299.         return getGroup(groupIndex).getName();     
  300.     }     
  301.      
  302.     /**   
  303.      * 取得全部组名   
  304.      * @return String[]   
  305.      */     
  306.     public String[] getGroupNames() {     
  307.         String sResult[] = new String[getGroupCount()];     
  308.         for (int i = 0; i < getGroupCount(); i++) {     
  309.             sResult[i] = getGroupName(i);     
  310.         }     
  311.         return sResult;     
  312.     }     
  313.      
  314.     /**   
  315.      * 取得当前组的总数   
  316.      * @return int   
  317.      */     
  318.     public int getGroupCount() {     
  319.         return groupList.size();     
  320.     }     
  321.      
  322.     /**   
  323.      * 往组中添加成员组件   
  324.      * @param groupIndex int 组的顺序号   
  325.      * @param member Component 成员组件   
  326.      */     
  327.     public void addMember(int groupIndex, Component member) {     
  328.         getGroup(groupIndex).addMember(getGroup(groupIndex).getMemberCount(),     
  329.                                        member);     
  330.     }     
  331.      
  332.     /**   
  333.      * 往组中插入成员组件   
  334.      * @param groupIndex int 组的顺序号   
  335.      * @param memberIndex int 插入的顺序号   
  336.      * @param member Component 成员组件   
  337.      */     
  338.     public void insertMember(int groupIndex, int memberIndex, Component member) {     
  339.         getGroup(groupIndex).addMember(memberIndex, member);     
  340.     }     
  341.      
  342.     /**   
  343.      * 从组中移除成员组件   
  344.      * @param groupIndex int   
  345.      * @param memberIndex int   
  346.      */     
  347.     public void removeMember(int groupIndex, int memberIndex) {     
  348.         getGroup(groupIndex).removeMember(memberIndex);     
  349.     }     
  350.      
  351.     /**   
  352.      * 取得成员组件   
  353.      * @param groupIndex int 组的顺序号   
  354.      * @param memberIndex int 成员组件的顺序号   
  355.      * @return Component 成员组件   
  356.      */     
  357.     public Component getMember(int groupIndex, int memberIndex) {     
  358.         return getGroup(groupIndex).getMember(memberIndex);     
  359.     }     
  360.      
  361.     /**   
  362.      * 取得全部成员组件   
  363.      * @param groupIndex int 组的顺序号   
  364.      * @return Component[] 全部成员组件   
  365.      */     
  366.     public Component[] getMembers(int groupIndex) {     
  367.         return getGroup(groupIndex).getMembers();     
  368.     }     
  369.      
  370.     /**   
  371.      * 取得成员组件的总数   
  372.      * @param groupIndex int 组的顺序号   
  373.      * @return int 总数   
  374.      */     
  375.     public int getMemberCount(int groupIndex) {     
  376.         return getGroup(groupIndex).getMemberCount();     
  377.     }     
  378.      
  379.     /**   
  380.      * 取得组   
  381.      * @param index int 组的顺序号   
  382.      * @return JGroupContainer 组   
  383.      */     
  384.     protected JGroupContainer getGroup(int index) {     
  385.         return (JGroupContainer) groupList.get(index);     
  386.     }     
  387.      
  388.     /**   
  389.      * 覆写的addImpl方法,禁止再向JGroupPane中添加组件   
  390.      * @param comp Component   
  391.      * @param constraints Object   
  392.      * @param index int   
  393.      */     
  394.     protected void addImpl(Component comp, Object constraints, int index) {     
  395.         if (forbidFlag) {     
  396.             if (! (comp instanceof JGroupContainer)) {     
  397.                 throw new UnsupportedOperationException(     
  398.                     "JGroupPane can't add component!");     
  399.             }     
  400.         }     
  401.         else {     
  402.             super.addImpl(comp, constraints, index);     
  403.         }     
  404.     }     
  405.      
  406.     /**   
  407.      * 

    Title: OpenSwing

      
     
  408.      * 

    Description: 组面板布局管理器

      
     
  409.      * 

    Copyright: Copyright (c) 2004

      
     
  410.      * 

    Company: 

      
     
  411.      * @author SunKing   
  412.      * @version 1.0   
  413.      */     
  414.     class GroupLayout     
  415.         implements LayoutManager, java.io.Serializable {     
  416.         int vgap = 0;     
  417.         int hgap = 0;     
  418.         public GroupLayout() {     
  419.         }     
  420.      
  421.         public GroupLayout(int hg, int vg) {     
  422.             this.hgap = hg;     
  423.             this.vgap = vg;     
  424.         }     
  425.      
  426.         public void addLayoutComponent(String name, Component comp) {     
  427.         }     
  428.      
  429.         public void removeLayoutComponent(Component comp) {     
  430.         }     
  431.      
  432.         public Dimension preferredLayoutSize(Container parent) {     
  433.             synchronized (parent.getTreeLock()) {     
  434.                 Insets insets = parent.getInsets();     
  435.                 int ncomponents = parent.getComponentCount();     
  436.                 int w = 0;     
  437.                 int h = 0;     
  438.                 for (int i = 0; i < ncomponents; i++) {     
  439.                     Component comp = parent.getComponent(i);     
  440.                     Dimension d = comp.getPreferredSize();     
  441.                     if (w < d.width) {     
  442.                         w = d.width;     
  443.                     }     
  444.                     h += d.height + vgap;     
  445.                 }     
  446.                 return new Dimension(insets.left + insets.right + w + 2 * hgap,     
  447.                                      insets.top + insets.bottom + h + 2 * vgap);     
  448.             }     
  449.         }     
  450.      
  451.         public Dimension minimumLayoutSize(Container parent) {     
  452.             return preferredLayoutSize(parent);     
  453.         }     
  454.      
  455.         public void layoutContainer(Container parent) {     
  456.             synchronized (parent.getTreeLock()) {     
  457.                 Insets insets = parent.getInsets();     
  458.                 int ncomponents = parent.getComponentCount();     
  459.                 if (ncomponents == 0) {     
  460.                     return;     
  461.                 }     
  462.                 int y = insets.top + vgap;     
  463.                 for (int c = 0; c < ncomponents; c++) {     
  464.                     int h = parent.getComponent(c).getPreferredSize().height;     
  465.                     parent.getComponent(c).setBounds(     
  466.                         insets.left + hgap,     
  467.                         y,     
  468.                         parent.getWidth() - insets.left - insets.right -     
  469.                         2 * hgap, h);     
  470.                     y += h + vgap;     
  471.                 }     
  472.             }     
  473.         }     
  474.      
  475.         public String toString() {     
  476.             return getClass().getName();     
  477.         }     
  478.     }     
  479.      
  480.     /**   
  481.      * 

    Title: OpenSwing

      
     
  482.      * 

    Description: 组

      
     
  483.      * 

    Copyright: Copyright (c) 2004

      
     
  484.      * 

    Company: 

      
     
  485.      * @author SunKing   
  486.      * @version 1.0   
  487.      */     
  488.     class JGroupContainer     
  489.         extends JPanel {     
  490.         private JButton bttGroupTitle = new JButton();     
  491.         private JPanel pMembers = new JPanel();     
  492.         private JScrollPane sp;     
  493.         public JGroupContainer() {     
  494.             this("");     
  495.         }     
  496.      
  497.         public JGroupContainer(String name) {     
  498.             this(name, UIManager.getColor("Desktop.background"));     
  499.         }     
  500.      
  501.         /**   
  502.          * @param name String  组名   
  503.          * @param background Color 成员组件所在面板背景色   
  504.          */     
  505.         public JGroupContainer(String name, Color background) {     
  506.             bttGroupTitle.setText(name);     
  507.             bttGroupTitle.setFocusable(false);     
  508.             pMembers.setLayout(new GroupLayout(55));     
  509.             this.setLayout(new BorderLayout());     
  510.             this.add(bttGroupTitle, BorderLayout.NORTH);     
  511.      
  512.             pMembers.setBackground(background);     
  513.      
  514.             Color thumbColor = UIManager.getColor("ScrollBar.thumb");     
  515.             Color trackColor = UIManager.getColor("ScrollBar.track");     
  516.             Color trackHighlightColor = UIManager.getColor(     
  517.                 "ScrollBar.trackHighlight");     
  518.      
  519.             UIManager.put("ScrollBar.thumb", background);     
  520.             UIManager.put("ScrollBar.track", background);     
  521.             UIManager.put("ScrollBar.trackHighlight", background);     
  522.             sp = new JScrollPane(pMembers);     
  523.             sp.setHorizontalScrollBarPolicy(     
  524.                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);     
  525.             this.add(sp, BorderLayout.CENTER);     
  526.             collapse();     
  527.             UIManager.put("ScrollBar.thumb", thumbColor);     
  528.             UIManager.put("ScrollBar.track", trackColor);     
  529.             UIManager.put("ScrollBar.trackHighlight", trackHighlightColor);     
  530.      
  531.         }     
  532.      
  533.         /**   
  534.          * 设置间距   
  535.          * @param hgap int 横间距   
  536.          * @param vgap int 竖间距   
  537.          */     
  538.         public void setMemberGap(int hgap, int vgap) {     
  539.             pMembers.setLayout(new GroupLayout(hgap, vgap));     
  540.         }     
  541.      
  542.         /**   
  543.          * 取得组的标题按钮   
  544.          * @return JButton   
  545.          */     
  546.         public JButton getTitleButton() {     
  547.             return bttGroupTitle;     
  548.         }     
  549.      
  550.         /**   
  551.          * 取得组的成员组件面板   
  552.          * @return JPanel   
  553.          */     
  554.         public JPanel getMembersContainer() {     
  555.             return pMembers;     
  556.         }     
  557.      
  558.         /**   
  559.          * 收缩组   
  560.          */     
  561.         public void collapse() {     
  562.             sp.setVisible(false);     
  563.             this.revalidate();     
  564.         }     
  565.      
  566.         /**   
  567.          * 展开组   
  568.          */     
  569.         public void expand() {     
  570.             sp.setVisible(true);     
  571.             this.revalidate();     
  572.         }     
  573.      
  574.         /**   
  575.          * 设置组名   
  576.          * @param name String 组名   
  577.          */     
  578.         public void setName(String name) {     
  579.             bttGroupTitle.setText(name);     
  580.         }     
  581.      
  582.         /**   
  583.          * 取得组名   
  584.          * @return String   
  585.          */     
  586.         public String getName() {     
  587.             return bttGroupTitle.getText();     
  588.         }     
  589.      
  590.         /**   
  591.          * 添加一个成员组件   
  592.          * @param index int 顺序号   
  593.          * @param c Component 成员组件   
  594.          */     
  595.         public void addMember(int index, Component c) {     
  596.             pMembers.add(c, index);     
  597.             pMembers.doLayout();     
  598.         }     
  599.      
  600.         /**   
  601.          * 删除一个成员组件   
  602.          * @param index int 顺序号   
  603.          */     
  604.         public void removeMember(int index) {     
  605.             pMembers.remove(index);     
  606.             pMembers.doLayout();     
  607.         }     
  608.      
  609.         /**   
  610.          * 取得一个成员组件   
  611.          * @param index int 顺序号   
  612.          * @return Component 成员组件   
  613.          */     
  614.         public Component getMember(int index) {     
  615.             return pMembers.getComponent(index);     
  616.         }     
  617.      
  618.         /**   
  619.          * 取得全部成员组件   
  620.          * @return Component[] 成员组件   
  621.          */     
  622.         public Component[] getMembers() {     
  623.             Component coms[] = new Component[getMemberCount()];     
  624.             for (int i = 0; i < coms.length; i++) {     
  625.                 coms[i] = pMembers.getComponent(i);     
  626.             }     
  627.             return coms;     
  628.         }     
  629.      
  630.         /**   
  631.          * 取得成员组件总数   
  632.          * @return int 总数   
  633.          */     
  634.         public int getMemberCount() {     
  635.             return pMembers.getComponentCount();     
  636.         }     
  637.      
  638.         /**   
  639.          * 重写的toString方法   
  640.          * @return String   
  641.          */     
  642.         public String toString() {     
  643.             return getName();     
  644.         }     
  645.     }     
  646.      
  647.     /**   
  648.          /**   
  649.       * 测试程序   
  650.       * @param args String[]   
  651.       */     
  652.      public static void main(String[] args) {     
  653. //         try {      
  654. //             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());      
  655. //         }      
  656. //         catch (Exception e) {      
  657. //         }      
  658.          JFrame frame = new JFrame();     
  659. //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
  660.          frame.setTitle("JGroupPanel Demo");     
  661.          frame.getContentPane().setLayout(new BorderLayout());     
  662.          JGroupPanel p = new JGroupPanel();     
  663.          frame.getContentPane().add(p, BorderLayout.CENTER);     
  664.      
  665.          frame.setSize(150600);     
  666.          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();     
  667.          frame.setLocation(d.width - frame.getSize().width - 10,     
  668.                            10);     
  669.          frame.setVisible(true);     
  670.      }     
  671.      
  672. }  

你可能感兴趣的:(JAVA)