简体   繁体   English

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

[英]Java Swing: Set starting time on timer and loop it

i'm using this example on leepoint.net 在leepoint.net上使用这个例子

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? 使用此代码,计时器在实时秒启动,但我想知道如何让它在1秒开始,然后让它运行10秒并重新开始? So from 1 to 10 and so on.. 所以从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));

                    }
    }

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM