简体   繁体   中英

Timer in Netbeans Swing GUI?

I'm creating a connect-4 game and i want to time the time it takes to finish the game (from when the player clicks the button start game, to when the congrats message is displayed). If it helps or makes a difference, there are multiple panels on our program.

thanks

There are many similar questions that will give you a great answer, have you tried searching stack overflow with slightly different phrasing of your question? For example you could look for "java time taken".


Here is one way to grab the total time taken:

//Grab the time when you start
long startTime = System.currentTimeMillis();

//Play game

//When you finish grab the current time and work out the time taken
long totalTime = System.currentTimeMillis() - startTime;    
//Convert into minutes and seconds
int seconds = (int) ((totalTime / 1000) % 60);
int minutes = (int) ((totalTime / 1000) / 60);
//Show time
System.out.println("Time taken: "+minutes+"m, "+seconds+"s");

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