java 如何让定时器返回时间
一旦我开始运行计时器,如何让它返回已经过去了多长时间?这个定时器可以设置可见吗?
Once I start running the timer, how do I get it to return how long has passed? Can this timer be set visible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为第二个参数传递给 Timer 构造函数的对象实现了 ActionListener。通过该接口,它会在计时器发出的每个警报时接收对其 actionPerformed 方法的调用。 actionPerformed 方法有一个 ActionEvent 参数,其方法 getWhen 给出时间戳。如果您希望 ActionListener 计算经过的时间,您需要将原始时间戳存储在可访问的地方。
Swing Timer 是一种没有自己的图形表示的机制。如果您愿意,您的 ActionListener 可以实现计时器的图形表示。它可以是一个图形组件,例如 JPanel,它的 actionPerformed 方法可以使其在每次计时器响起时做出明显的响应,也许是通过绘制一些东西。
The object that you pass as second argument to the Timer constructor implements ActionListener. Through that interface it receives a call to its actionPerformed method upon each alarm from the timer. The actionPerformed method has an ActionEvent parameter whose method getWhen gives the timestamp. You will need to store the original timestamp somewhere accessible if you want the ActionListener to compute the elapsed time.
A Swing Timer is a mechanism with no graphical representation of its own. If you like, your ActionListener can implement a graphical representation of the timer. 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.