2011-12-14 36 views

回答

0

一种方式来做到这一点是通过覆盖的JProgressBargetString()方法:

public class CustomProgressBar extends javax.swing.JProgressBar { 

    @Override 
    public String getString() { 
     return getValue() + ""; 
    } 

} 

假设你分别设置最大值和最小值为0,32,并使用setValue()方法的进步的价值观,你的进度条现在将显示从0整数32

这里是一个工作示例:

import javax.swing.JOptionPane; 
import javax.swing.SwingWorker; 

public class ProgressBarExample { 

    public static void main(String[] args) { 
     final CustomProgressBar customProgressBar = new CustomProgressBar(); 
     customProgressBar.setMaximum(32); 
     customProgressBar.setStringPainted(true); 
     new SwingWorker<Void, Void>() { 
      @Override 
      protected Void doInBackground() throws Exception { 
       int value = 0; 
       Thread.sleep(1500); 
       while (value < customProgressBar.getMaximum()) { 
        Thread.sleep(250); 
        value ++; 
        customProgressBar.setValue(value); 
       } 
       return null; 
      } 
     }.execute(); 
     JOptionPane.showMessageDialog(null, customProgressBar, "Progress bar example", JOptionPane.PLAIN_MESSAGE); 
    } 

}