Java Swing:在计时器上设置开始时间并循环播放 [英] Java Swing: Set starting time on timer and loop it
本文介绍了Java Swing:在计时器上设置开始时间并循环播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这个 leepoint.net 上的示例一个>
使用此代码,计时器以实时秒数启动,但我想知道如何实现从 1 秒开始,然后让它运行 10 秒并重新开始?所以从 1 到 10 等等..
with this code the timer starts on real time seconds, but i was wondering how i can make it start at 1 second and then let it run up to 10 seconds and start over? So from 1 to 10 and so on..
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:在计时器上设置开始时间并循环播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文