int Hours=2;
int Minutes=30;
int Seconds=0;DecimalFormat fmt = new DecimalFormat("00");
System.out.println(fmt.format(Hours) + ":" + fmt.format(Minutes) + ":" + fmt.format(Seconds));

解决方案 »

  1.   

    用Date类型的变量,有个预格式化方法。
      

  2.   

    s = s.format("%02d:%02d:%02d", 1, 2, 3);
      

  3.   

    说明:是JDK1.5
    s = s.format("%02d:%02d:%02d", Hours, Minutes, Seconds);
      

  4.   

    最笨的写了个方法:

    public static String format(int num, int precision){
    String fN = "" + num;
    for (int i = precision;i > fN.length(); i--){
    fN = "0" + fN;
    }
    return fN;
    }System.out.println(format(Hours, 2)+":"+ format(Minutes, 2)+":"+format(Seconds, 2));
      

  5.   

    推荐用:DecimalFormat fmt = new DecimalFormat("00");
    System.out.println(fmt.format(Hours) + ":" + fmt.format(Minutes) + ":" + fmt.format(Seconds));当然要加:import java.text.DecimalFormat;
      

  6.   

    //谢谢大家参与,把我试验的小玩意和大家共享
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    public class ProgressBarExample1 {
    public static void main(String[] args) {
    JFrame f = new JFrame("Progress Bar Example 1");
    int Hours=0;
    int Minutes=00;
    int Seconds=50;
    final int AllTime=Hours*3600+Minutes*60+Seconds;
    final JProgressBar b = new JProgressBar();
    b.setOrientation(JProgressBar.HORIZONTAL);
    b.setStringPainted(true);
    b.setString(Hours+":"+Minutes+":"+Seconds);
    b.setMinimum(0);
    b.setMaximum(AllTime);
    b.setValue(0);
    b.setForeground(Color.blue);
    b.setBorderPainted(true);
    f.getContentPane().add(b);
    f.pack();
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent evt) {
    System.exit(0);
    }
    }); Timer t = new Timer(100, new ActionListener() {

    int currentTime=AllTime;
    int currentHours=0;
    int currentMinutes=0;
    int currentSeconds=0;
    public void actionPerformed(ActionEvent evt) {
    if (--currentTime>= 0) {
    DecimalFormat fmt = new DecimalFormat("00");
    currentHours=currentTime/3600;
    currentMinutes=(currentTime%3600)/60;
    currentSeconds=(currentTime%3600)%60;
    b.setString(fmt.format(currentHours)+":"+fmt.format(currentMinutes)+":"+fmt.format(currentSeconds));
    b.setValue(currentTime);
    } else {
    currentTime=AllTime;
    //((Timer)evt.getSource()).stop();
    }
    }
    });
    t.start();
    }
    }
      

  7.   

    比最笨的还要笨一点点:
    public String format(int time)
    {
            String str = "" + time;
            if ( time < 9)
                   str = "0" + time;
            return str;
    }System.out.println(format(Hours) + ":" + format(Minutes) + ":" +format(Seconds));
      

  8.   

    如果要进度条反向增长,替换下面的两句
    int currentTime=0; //int currentTime=AllTime;
    if (++currentTime<= AllTime) {       //if (--currentTime>= 0) {
      

  9.   

    to: rower203(华仔)你的程序我运行了一下,当传入的参数为format(Hours, 3)时,输出的时间还是02,而不是002,请问这是为什么啊!?
      

  10.   

    也就是说s="0"+"0"+"abc"输出的结果是0abc而不是00abc,请问这种情况如何解决!!@?