2011-04-02 94 views
0

我使用这个example on leepoint.net的Java Swing:设置定时器和循环开始时间

与此代码计时器实时秒开始,但我想知道我怎么可以把它 以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)); 

        } 
    } 
+0

为了更好地帮助越早,张贴[SSCCE(http://pscode.org/sscce.html)(具有合理和一致的缩进)。 – 2011-04-02 16:10:59

+0

感谢您的评论我会记住下一个问题,现在对不起 – Opoe 2011-04-02 17:56:12

回答

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

谢谢我的意思 – Opoe 2011-04-02 17:55:34