简体   繁体   English

如何获取计时器返回时间的java

[英]How to get the timer to return time java

Once I start running the timer, how do I get it to return how long has passed? 一旦开始运行计时器,如何获得返回的时间? Can this timer be set visible? 可以将此计时器设置为可见吗?

The object that you pass as second argument to the Timer constructor implements ActionListener. 您作为第二个参数传递给Timer构造函数的对象将实现ActionListener。 Through that interface it receives a call to its actionPerformed method upon each alarm from the timer. 通过该接口,它会在计时器发出的每个警报中收到对其actionPerformed方法的调用。 The actionPerformed method has an ActionEvent parameter whose method getWhen gives the timestamp. actionPerformed方法具有一个ActionEvent参数,该方法的getWhen方法提供时间戳。 You will need to store the original timestamp somewhere accessible if you want the ActionListener to compute the elapsed time. 如果希望ActionListener计算经过的时间,则需要将原始时间戳存储在可访问的位置。

A Swing Timer is a mechanism with no graphical representation of its own. Swing Timer是一种没有自己的图形表示的机制。 If you like, your ActionListener can implement a graphical representation of the timer. 如果愿意,您的ActionListener可以实现计时器的图形表示。 It can be a graphic component such as a JPanel and its actionPerformed method can cause it to respond visibly, perhaps by drawing something, each time that the timer goes off. 它可以是诸如JPanel之类的图形组件,并且它的actionPerformed方法可以使它在每次计时器关闭时进行可见的响应,也许是通过绘制一些东西。

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

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