Java Swing : Set starting time on timer and loop it

标签 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));

                    }
    }

最佳答案

试试这个

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/

相关文章:

java - Cassandra 在启动时生成 “java.lang.OutOfMemoryError: Java heap space” 错误

java - JProgressBar 未更新

java - JComboBox运行3次后给出一个数组[jdbc]

php - C 中的 strtotime 函数

java - 对于使用现有库的 Web 应用程序来说,最安全的日志记录框架是什么?

algorithm - 数学公式的比较

java - 如何使用 Grails oauth 插件获取访问 token ?

java - 用 Java 为简单的棋盘游戏创建图形

javascript - 计算资源的可用性

java - 从转换毫秒显示小时数显示不正确