代码演示
import java. awt. * ;
import java. awt. event. * ;
import javax. swing. * ;
public class a {
public static void main ( String [ ] args) {
new a ( ) ;
}
public a ( ) {
JFrame JF = new JFrame ( "测试" ) ;
JCheckBox JC1 = new JCheckBox ( "不确定进度" ) ;
JCheckBox JC2 = new JCheckBox ( "不绘制边框" ) ;
JProgressBar bar = new JProgressBar ( JProgressBar . HORIZONTAL, 0 , 100 ) ;
JC1. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
bar. setIndeterminate ( JC1. isSelected ( ) ) ;
bar. setStringPainted ( ! JC1. isSelected ( ) ) ;
}
} ) ;
JC2. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
bar. setBorderPainted ( ! JC2. isSelected ( ) ) ;
}
} ) ;
Box B = Box . createVerticalBox ( ) ;
B . add ( JC1) ;
B . add ( JC2) ;
bar. setStringPainted ( true ) ;
bar. setBorderPainted ( true ) ;
JF. setLayout ( new FlowLayout ( ) ) ;
JF. add ( B ) ;
JF. add ( bar) ;
JF. setSize ( 600 , 500 ) ;
JF. setDefaultCloseOperation ( 3 ) ;
JF. setLocationRelativeTo ( null ) ;
JF. setResizable ( false ) ;
JF. pack ( ) ;
JF. setVisible ( true ) ;
for ( int i = 0 ; i <= 100 ; i++ ) {
bar. setValue ( i) ;
try {
Thread . sleep ( 1000 ) ;
} catch ( InterruptedException e1) {
e1. printStackTrace ( ) ;
}
}
}
}
运行结果