【问题标题】:Java Swing: Set starting time on timer and loop itJava Swing:在计时器上设置开始时间并循环它
【发布时间】:2011-04-02 16:00:58
【问题描述】:

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

                    }
    }

【问题讨论】:

  • 为了尽快获得更好的帮助,请发布SSCCE(带有逻辑和一致的缩进)。
  • 感谢您的评论,下一个问题我会记住这一点,现在很抱歉

标签: java swing time timer set


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多