Java Swing:在计时器上设置开始时间并循环它

时间:2011-04-02 16:00:59

标签: java swing time timer set

我正在使用此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));

                    }
    }

1 个答案:

答案 0 :(得分:2)

试试这个

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);
    }
}