1 public class JProgressBarTest extends JFrame{ 2 3 public JProgressBarTest() { 4 super(); 5 setTitle("表格"); 6 setBounds(100,100,350,150); 7 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 8 final JButton button = new JButton(" "); 9 final JButton button2 = new JButton("完成"); 10 button2.setEnabled(false); //初始化时不可用 11 button.setBounds(100,100,100,100); 12 13 final JProgressBar progressBar = new JProgressBar(); 14 progressBar.setStringPainted(true); //显示提示信息 15 progressBar.setIndeterminate(false); //确定进度的进度条 16 //progressBar.setIndeterminate(true); //不确定进度的进度条 17 // progressBar.setString("升级中..."); //确定信息时加上此条,则提示升级中,没有%比,如是不加上这个,则会提示% 18 setLayout(new FlowLayout(2,10,10)); 19 getContentPane().add(button); //布局处理 20 getContentPane().add(button2); //布局处理 21 getContentPane().add(progressBar); //布局处理 22 new Progress(progressBar,button2).start(); //自定义类progress 23 } 24 25 /** 26 * @param args 27 */ 28 public static void main(String[] args) { 29 // TODO Auto-generated method stub 30 JProgressBarTest jProgressBarTest = new JProgressBarTest(); 31 jProgressBarTest.setVisible(true); 32 } 33 34 } 35 class Progress extends Thread{//自定义类progress 36 private final int []progressValue = {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60, 37 62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100}; 38 private JProgressBar progressBar; 39 private JButton button; 40 public Progress(JProgressBar progressBar,JButton button) 41 { 42 this.progressBar = progressBar; 43 this.button =button; 44 } 45 public void run() 46 { 47 for(int i=0;i<progressValue.length;i++) 48 { 49 try 50 { 51 Thread.sleep(100); 52 }catch(Exception e) 53 { 54 e.printStackTrace(); 55 } 56 progressBar.setValue(progressValue[i]); //进度值 57 } 58 progressBar.setIndeterminate(false); //采用确定的进度条 59 //progressBar.setIndeterminate(true); //不确定进度的进度条 60 progressBar.setString("升级完成."); //提示信息 61 button.setEnabled(true); //按钮可用 62 } 63 }
摘录自:null