JAVA進(jìn)行式
          讓我們把JAVA進(jìn)行到底!
          posts - 13,comments - 21,trackbacks - 0

                  工具欄ToolBar中可加入如JLabel、JPanel、JButton或JComboBox等很多可視化組件的對(duì)象,且默認(rèn)為FlowLayout布局管理器,有時(shí)候因?yàn)榫幊痰男枰覀円锩娌迦牒芏鄠€(gè)組件,如何顯示出完整的組件列表成了一個(gè)問(wèn)題,而ToolBar的對(duì)象放在JScrollPane對(duì)象中是顯示不出來(lái)的,原因是它和JPanel一樣是沒(méi)有邊框的,我的解決辦法是,將要插入到工具欄的對(duì)象先放入集合中(如果全是相同類型的對(duì)象,放在數(shù)組中保存更好),再通過(guò)for循環(huán)依次加入到ToolBar對(duì)象中,在ToolBar外面設(shè)置一個(gè)按鈕,編寫響應(yīng)該按鈕的點(diǎn)擊事件,當(dāng)該按鈕被點(diǎn)擊一次時(shí),通過(guò)數(shù)組下標(biāo)(或集合索引)從ToolBar中remove()去掉一個(gè)組件,并調(diào)用revalidate()方法,讓緊跟其后的組件移上去,添補(bǔ)刪除后留下的空白,并把刪除的那個(gè)組件重新用add()方法追加到工具欄尾部,這樣循環(huán)刪除和添加,就實(shí)現(xiàn)了工具欄的滾動(dòng)效果,由于工具欄的FlowLayout布局,使它在未設(shè)置時(shí)只能向一個(gè)方向添加組件,故我的這個(gè)工具欄的滾動(dòng)效果是單向的。
                  對(duì)于兩向滾動(dòng),我考慮在反向滾動(dòng)前重新設(shè)置工具欄的布局管理器,以改變其添加組件的方向,從而達(dá)到雙向滾動(dòng)的效果。具體能否實(shí)現(xiàn),有待進(jìn)一步編碼檢測(cè)。
                  在這里我把單向滾動(dòng)的源代碼記錄如下:

          /**DiaryEditToolBarPane類構(gòu)建一個(gè)包含一個(gè)工具欄和一個(gè)控制滾動(dòng)的按鈕 */

          public class DiaryEditToolBarPane extends JPanel implements ActionListener
          {
           JTextPane jTextPane;
           DiaryEditToolBar diaryEditToolBar;
           JPanel jPanelSouth = new JPanel();
           JButton jButtonUp = new JButton(ConstValue.IMG_UP);
           //JButton jButtonDn = new JButton(ConstValue.IMG_DN);
           
           public DiaryEditToolBarPane(JTextPane jTextPane)
           {
            this.jTextPane = jTextPane;
            diaryEditToolBar = new DiaryEditToolBar(this.jTextPane);
            
            jButtonUp.setMargin(new Insets(0,0,0,0));
            //jButtonDn.setMargin(new Insets(0,0,0,0));
            jButtonUp.setActionCommand("up");
            //jButtonDn.setActionCommand("down");
            //jPanelSouth.setLayout(new GridLayout(2,1));
            jPanelSouth.add(jButtonUp);
            //jPanelSouth.add(jButtonDn);
            
            this.setLayout(new BorderLayout());
            this.add(diaryEditToolBar,BorderLayout.CENTER);
            this.add(jPanelSouth, BorderLayout.SOUTH);
            
            jButtonUp.addActionListener(this);
            //jButtonDn.addActionListener(this);
           }
            
           public void setTextPane(JTextPane jTextPane)
           {
            this.jTextPane = jTextPane;
            diaryEditToolBar.setTextPane(this.jTextPane);
           }

           public void actionPerformed(ActionEvent e)
           {
            if(e.getActionCommand().equals("up"))
            {
             diaryEditToolBar.moveUp();
            }
            /*
            else if(e.getActionCommand().equals("down"))
            {
             diaryEditToolBar.moveDown(); 
            }*/
           }
          }

          /**定義一個(gè)具體實(shí)現(xiàn)工具欄組件布局與滾動(dòng)的類DiaryEditToolBar  */

          public class DiaryEditToolBar extends JToolBar
          {
           /**用于保存工具按鈕的對(duì)象數(shù)組*/
           private BrimlessButton[] toolBarButton;
           /**工具按鈕增、刪索引值*/
           private static int index = 0;
           
            
           private void init()
           {
            this.setFloatable(false);
            BrimlessButton[] temp =
            {
             toolBarButtonSave,toolBarButtonSave,toolBarButtonCut,
             toolBarButtonCopy,toolBarButtonPaste,toolBarButtonFontStyle,
             toolBarButtonFontSize,toolBarButtonFontForeground,toolBarButtonBackground,
             toolBarButtonBold,toolBarButtonItalic,toolBarButtonUnderline,
             toolBarButtonFlusLeft,toolBarButtonCenter,toolBarButtonFlushRight,
             toolBarButtonUndo,toolBarButtonRedo,toolBarButtonIndentLeft,
             toolBarButtonIndentRight,toolBarButtonURL,toolBarButtonImage,
             toolBarButtonDateTime
            };
            toolBarButton = temp;
            
            for(int i=0; i<22; i++)
            {
             this.addElement(toolBarButton[i]);
            }

           }

           private void addElement(JButton jButton)
           {
            this.add(jButton);
           }
           
           private void delElement(JButton jButton)
           { 
            this.remove(jButton);
           }
           
           /**刪除工具欄開頭的工具按鈕,并將其添加到工具欄末尾,以實(shí)現(xiàn)工具欄向上滾動(dòng)的效果*/
           public void moveUp()
           {
            delElement(toolBarButton[index]);
            this.revalidate();
            addElement(toolBarButton[index]);
            index = (++index)%22;
           }
           
           /**增加工具欄開頭的工具按鈕,以實(shí)現(xiàn)工具欄向下滾動(dòng)的效果*/
           public void moveDown()
           {
            //addElement(toolBarButton[index]);
            //this.revalidate();
            //repaint();
           } 
          }

          posted on 2005-11-29 23:18 水秀清靈 閱讀(1188) 評(píng)論(0)  編輯  收藏 所屬分類: 學(xué)習(xí)筆記
          主站蜘蛛池模板: 宿松县| 轮台县| 咸阳市| 溆浦县| 岳池县| 宜阳县| 水富县| 曲松县| 台东县| 浮山县| 辉南县| 锡林浩特市| 和林格尔县| 宣汉县| 三亚市| 黄浦区| 霍山县| 深泽县| 平昌县| 天台县| 易门县| 遵化市| 五华县| 富蕴县| 金溪县| 江川县| 隆化县| 大庆市| 玉溪市| 甘泉县| 禄劝| 彭阳县| 隆化县| 威宁| 游戏| 合水县| 安乡县| 鲁山县| 灵丘县| 东乡族自治县| 甘德县|