進(jìn)度條,是我們平常所用并不是很多的Swing控件之一,雖然是控件,但是我們的應(yīng)用次數(shù)不多,雖然通常掌握程度也就不夠熟練。下面,介紹一個例子。 首先創(chuàng)建一個JFrame,添加一個JTextArea和一個JProgressBar,還有一個JButton 因為要使用到多線程,所以在JFrame后面加上implements Runnable。如下: public class ProgressBarDemo extends new javax.swing.JFrame implements Runnable{ int leng=0; boolean a=false; } 在按鈕中添加如下代碼: if(a==false){ a=true; this.jButton.setText("Stop"); }else{ this.jButton.setText("Start"); }
Thread t=new Thread(this);
t.start();
下面是run()函數(shù)代碼: public void run() { while(true){ if(a==true){ if(this.jProgressBar1.getValue()<this.jProgressBar1.getMaximum()){ this.jProgressBar1.setValue(this.jProgressBar1.getValue()+1); this.jTextArea1.append(tt.substring(leng,leng+1)); leng++; if(leng==tt.length()){ leng=0; } }else{ a=false; }try { t.sleep(50); } catch (InterruptedException ex) { ex.printStackTrace(); } } } }
|