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));
int Minutes=30;
int Seconds=0;DecimalFormat fmt = new DecimalFormat("00");
System.out.println(fmt.format(Hours) + ":" + fmt.format(Minutes) + ":" + fmt.format(Seconds));
s = s.format("%02d:%02d:%02d", Hours, Minutes, Seconds);
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));
System.out.println(fmt.format(Hours) + ":" + fmt.format(Minutes) + ":" + fmt.format(Seconds));当然要加:import java.text.DecimalFormat;
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();
}
}
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));
int currentTime=0; //int currentTime=AllTime;
if (++currentTime<= AllTime) { //if (--currentTime>= 0) {