Question: Write a Java program with a Graphical User Interface that will function as a stopwatch. The GUI only needs a button and a label to
Write a Java program with a Graphical User Interface that will function as a stopwatch. The GUI only needs a button and a label to display the elapsed time. The program will need a class instance variable called startTime to keep track of the time when the start button was pressed. This value should be a long, which is just like an int, but can store much larger numbers. It should be initialized to zero. The actionPerformed method should be structured as shown below. Note in the IF statement there are two equals signs. public void actionPerformed (java. awt. event. ActionEvent thing) { (if (startTime == 0) { // create an object of the java.util.Date class // call the getTime () method on the Date object and save the returned value in startTime // change the text of the button } else { // create an object of the java.util.Date class // call the getTime () method on the Date object. Subtract startTime from the returned value to get the elapsed time. // Convert the elapsed time to a double and divide by 1000 to get seconds // Display the result // change the text of the button // set startTime to zero } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
