我正在使用这个example on leepoint.net
使用这段代码,计时器以实时秒数启动,但我想知道如何做到这一点 从 1 秒开始,然后让它运行到 10 秒并重新开始? 所以从 1 到 10 等等..
class ClockListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Calendar now = Calendar.getInstance();
int s = now.get(Calendar.SECOND);
_timeField.setText(String.format("%1$tS", now));
}
}
最佳答案
试试这个
class ClockListener implements ActionListener {
int count = 0;
public void actionPerformed(ActionEvent e) {
int fakeSecond = (count++ % 10) + 1;
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
_timeField.setText("" + h + ":" + m + ":" + fakeSecond);
}
}
关于Java Swing : Set starting time on timer and loop it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524222/