1,进度条实例
?
?
2,创建一个进度条
copyProgressBar = new JProgressBar(); copyProgressBar.setBorderPainted(true); copyProgressBar.setString(PROGRESSINITSTR); copyProgressBar.setStringPainted(true);
?
?
3,进度条两种模式
模式一:百分比模式(determinate mode)
更新进度条:
/*** * 测试进度条<br>百分比 */ public void testProgressBar() { copyProgressBar.setIndeterminate(false);//取消"不确定模式" copyProgressBar.setString(PROGRESSINITSTR); copyProgressBar.setStringPainted(true); new Thread(new Runnable() { @Override public void run() { copyFileBtn.setEnabled(false); int count = 0; copyProgressBar.setForeground(progressDefaultColor); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } while (true) { if(isStop()){ setStop(false); break; } count++; try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } copyProgressBar.setString(count + "%"); copyProgressBar.setValue(count); if (count >= 100) { copyProgressBar.setForeground(Color.GREEN); break; } } copyFileBtn.setEnabled(true); } }).start(); }
?
?
?
模式二:不确定模式
?启动进度条:
/*** * 左右移动<br> * 将进度条设置为不确定模式 */ public void testIndeterminate() { copyProgressBar.setStringPainted(false); copyProgressBar.setIndeterminate(true); }
?
?
4,说明
(1)copyProgressBar.setString(PROGRESSINITSTR);
copyProgressBar.setStringPainted(true);
作用:在进度条上显示百分比的文字
(2)停止不确定模式:
public void stopIndeterminate(){ copyProgressBar.setIndeterminate(false); copyProgressBar.setValue(100); }
?
源代码:
http://pan.baidu.com/s/1i3s9cFF
参考:http://hw1287789687.iteye.com/blog/2003105
原文:http://hw1287789687.iteye.com/blog/2246665