進度條的使用,我的編程中一直沒有使用到這個組件,但是應該其在程序的啟動過程中是非常重要的一個組件。
           1 ////////////////////////////////////////////////////////////////////////////////////////////
           2 //
           3 //  @name TestJProgressBar.java
           4 //
           5 //  @discription 進度條演示程序
           6 //
           7 //  @author hcm
           8 //
           9 //  @date 2006-12
          10 //
          11 /////////////////////////////////////////////////////////////////////////////////////////////
          12 import java.awt.BorderLayout;
          13 import java.awt.Dimension;
          14 import javax.swing.*;
          15 import java.awt.event.*;
          16 import javax.swing.event.ChangeEvent;
          17 import javax.swing.event.ChangeListener;
          18 
          19 public class TestJProgressBar implements ActionListener ,ChangeListener
          20 {
          21     private JPanel panel;
          22     private Timer timer;
          23     private JButton btn;
          24     private JProgressBar pgb;
          25     private JLabel label;
          26     public void init()
          27     {
          28          panel = new JPanel();
          29          btn = new JButton("start");
          30          btn.addActionListener(this);
          31          panel.add (btn,BorderLayout.CENTER);
          32          
          33          timer = new Timer(100,this);
          34          
          35          pgb = new JProgressBar();
          36          pgb.setOrientation (JProgressBar.HORIZONTAL);
          37          pgb.setMaximum (100);
          38          pgb.setMinimum (0);
          39          pgb.setValue (0);
          40          pgb.setStringPainted (true);
          41          pgb.addChangeListener (this);
          42          pgb.setPreferredSize (new Dimension(400,20));
          43          
          44          label = new JLabel("");
          45      
          46         JFrame frame = new JFrame("test");
          47         frame.add (panel,BorderLayout.NORTH);
          48         frame.add (pgb,BorderLayout.CENTER);
          49         frame.add (label,BorderLayout.SOUTH);    
          50         frame.setSize(400,100);
          51         frame.setVisible (true);
          52         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
          53     }
          54     public static void main(String args[])
          55     {
          56         new TestJProgressBar().init ();      
          57     }
          58     public void actionPerformed(ActionEvent e)
          59     {
          60         if(e.getSource ()==btn)
          61         {
          62             timer.start ();
          63             System.out.println ("come here!");
          64         }
          65         if(e.getSource ()==timer)
          66         {
          67             System.out.println ("come here too!");
          68            int value = pgb.getValue () ;
          69            if(value<100)
          70            {
          71                value++;
          72                pgb.setValue (value);
          73            }
          74            if(value ==100)
          75            {
          76                timer.stop ();
          77                pgb.setValue (0);
          78            }
          79             
          80         }
          81     }
          82     public void stateChanged(ChangeEvent e)
          83     { 
          84          System.out.println ("come here too2!");
          85         if(e.getSource ()==pgb)
          86         {
          87              int value = pgb.getValue ();  
          88             label.setText ("以完成:"+value+"%");
          89         }
          90     }
          91 }
          posted on 2007-02-06 16:21 -274°C 閱讀(635) 評論(1)  編輯  收藏 所屬分類: JAVA


          FeedBack:
          # re: 進度條演示程序
          2007-02-06 16:42 | sdasdsdasd
          asdasdasdasd  回復  更多評論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916109
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 六枝特区| 德庆县| 菏泽市| 会泽县| 星座| 青神县| 青龙| 通州市| 安乡县| 巧家县| 和顺县| 北碚区| 沐川县| 当阳市| 樟树市| 涡阳县| 鄂尔多斯市| 司法| 白银市| 马公市| 张家界市| 宁海县| 三都| 陆丰市| 东光县| 中江县| 东至县| 墨江| 砀山县| 田东县| 北辰区| 依兰县| 尼勒克县| 莱州市| 衡东县| 连云港市| 蓬溪县| 徐闻县| 天峨县| 文昌市| 山东省|