你必须为此使用线程。设计一个实现
Runnable接口的类,该接口将更新这样的值。
class ProgressBarUpdator implements java.lang.Runnable { private javax.swing.JProgressBar jpb = null; private java.lang.Integer value = null; public ProgressBarUpdator(javax.swing.JProgressBar jpb) { this.jpb = jpb; jpb.setMaximum(100); } public void setValue(java.lang.Integer value) { this.value = value; } public void run() { do { if (value != null) { jpb.setValue((int)java.lang.Math.round(java.lang.Math.floor(value.intValue() * 100 / maximum))); } try { java.lang.Thread.sleep(100L); } catch (java.lang.InterruptedException ex) { ex.printStackTrace(); } } while (value == null || value.intValue() < jpb.getMaximum()); }}
并在你的框架类中progressBar与新类一起使用
ProgressBarUpdator ju = new ProgressBarUpdator(progressBar);new java.lang.Thread(ju).start();
每当你想更改值时,只需使用以下语句
ju.setValue([Value to set]);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)