如果两次提出同样的问题被认为是垃圾邮件,我真的很抱歉,因为我已经在一小时前询问了反向计时器。
但现在新的问题是,我再也无法引起任何人对这个问题的关注。我已经成功编码了计时器,感谢你们,但后来我试着将秒转换为hh:mm:ss格式,但它没有用。而不是继续前进到00:00:00。它只显示我编码的时间,就是这样。
这是我的代码。
import java.util.Timer;
import java.util.TimerTask;
public class countdown extends javax.swing.JFrame {
public countdown() {
initComponents();
Timer timer;
timer = new Timer();
timer.schedule(new DisplayCountdown(), 0, 1000);
}
class DisplayCountdown extends TimerTask {
int seconds = 5;
int hr = (int)(seconds/3600);
int rem = (int)(seconds%3600);
int mn = rem/60;
int sec = rem%60;
String hrStr = (hr<10 ? "0" : "")+hr;
String mnStr = (mn<10 ? "0" : "")+mn;
String secStr = (sec<10 ? "0" : "")+sec;
public void run() {
if (seconds > 0) {
lab.setText(hrStr+ " : "+mnStr+ " : "+secStr+"");
seconds--;
} else {
lab.setText("Countdown finished");
System.exit(0);
}
}
}
public static void main(String args[]) {
new countdown().setVisible(true);
}
答案 0 :(得分:16)
移动您的计算
int hr = seconds/3600;
int rem = seconds%3600;
int mn = rem/60;
int sec = rem%60;
String hrStr = (hr<10 ? "0" : "")+hr;
String mnStr = (mn<10 ? "0" : "")+mn;
String secStr = (sec<10 ? "0" : "")+sec;
进入run
方法。
答案 1 :(得分:1)
public String getCountDownStringInMinutes(int timeInSeconds)
{
return getTwoDecimalsValue(timeInSeconds/3600) + ":" + getTwoDecimalsValue(timeInSeconds/60) + ":" + getTwoDecimalsValue(timeInSeconds%60);
}
public static String getTwoDecimalsValue(int value)
{
if(value>=0 && value<=9)
{
return "0"+value;
}
else
{
return value+"";
}
}