Site icon FahmidasClassroom

Create Stopwatch using Java (Form Design by Source Code)

Stopwatch1

The way to create a stopwatch using Java has been shown in this tutorial. Here, the JFrame form has been used to create the interface of the stopwatch. Follow the steps given below to create the application.
Steps:
01. Create a new Java application named stopwatch.
02. Create a JFrame named StopWatch1.
03. Import the Java packages

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

04. Write the following code in the constructor of the class to set the Form title and size.

setTitle("Stopwatch Application");
setSize(300, 100);

05. Declare the following class variables.

long startTime;
long stopTime;
double elapsedTime;

06. Declare three Button controls.


JButton startButton = new JButton();
JButton stopButton = new JButton();
JButton exitButton = new JButton();

07. Declare three label controls.

JLabel startLabel = new JLabel();
JLabel stopLabel = new JLabel();
JLabel elapsedLabel = new JLabel();

08. Declare three text field controls.

JTextField startTextField = new JTextField();
JTextField stopTextField = new JTextField();
JTextField elapsedTextField = new JTextField();

09. Declare GridBagLayout() and set the positions of the controls into the form.

getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gridConstraints = new GridBagConstraints();

startButton.setText("Start Timing");
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
getContentPane().add(startButton, gridConstraints);

stopButton.setText("Stop Timing");
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
getContentPane().add(stopButton, gridConstraints);

exitButton.setText("Exit");
gridConstraints.gridx = 0;
gridConstraints.gridy = 2;
getContentPane().add(exitButton, gridConstraints);

startLabel.setText("Start Time");
gridConstraints.gridx = 1;
gridConstraints.gridy = 0;
getContentPane().add(startLabel, new GridBagConstraints());

stopLabel.setText("Stop Time");
gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
getContentPane().add(stopLabel, gridConstraints);

elapsedLabel.setText("Elapsed Time (sec)");
gridConstraints.gridx = 1;
gridConstraints.gridy = 2;
getContentPane().add(elapsedLabel, gridConstraints);

startTextField.setText("");
startTextField.setColumns(15);
gridConstraints.gridx = 2;
gridConstraints.gridy = 0;
getContentPane().add(startTextField, new GridBagConstraints());

stopTextField.setText("");
stopTextField.setColumns(15);
gridConstraints.gridx = 2;
gridConstraints.gridy = 1;
getContentPane().add(stopTextField, gridConstraints);

elapsedTextField.setText("");
elapsedTextField.setColumns(15);
gridConstraints.gridx = 2;
gridConstraints.gridy = 2;
getContentPane().add(elapsedTextField, gridConstraints);
pack();

10. Declare method for the start button.

private void startButtonActionPerformed(ActionEvent e)
{
     //click of start timing button
     startTime = System.currentTimeMillis();
     startTextField.setText(String.valueOf(startTime));
     stopTextField.setText("");
     elapsedTextField.setText("");
}

11. Declare method for the stop button.

private void stopButtonActionPerformed(ActionEvent e)
{
      // click of stop timing button
      stopTime = System.currentTimeMillis();
      stopTextField.setText(String.valueOf(stopTime));
      elapsedTime = (stopTime - startTime) / 1000.0;
      elapsedTextField.setText(String.valueOf(elapsedTime));
}

11. Declare method for the exit button.

private void exitButtonActionPerformed(ActionEvent e)
{
      System.exit(0);
}

12. Add code to call the methods of the buttons.

startButton.addActionListener(this::startButtonActionPerformed);
stopButton.addActionListener(this::stopButtonActionPerformed);
exitButton.addActionListener(this::exitButtonActionPerformed);

13. Compile the source code and run the application.
***The source code is taken from “Learn Java GUI Applications” book.

Exit mobile version