简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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