进度条的使用,我的编程中一直没有使用到这个组件,但是应该其在程序的启动过程中是非常重要的一个组件。
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
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 }
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 }