如果问同一个问题两次被认为是垃圾邮件,我真的很抱歉,因为我在一小时前已经问过倒计时。
但现在新的问题是,虽然我无法再次引起任何人对那个问题的关注。感谢你们,我已经成功地编写了计时器,但后来我尝试将秒数转换为 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);
}
最佳答案
移动你的计算
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
方法。
关于java - 如何将Timer的秒数转换为hh :mm:ss?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19205920/