How to create a simple digital timer using Java

To create a simple digital timer in Java, we can use javax.swing for the GUI components and java.util.Timer or javax.swing.Timer for the timer functionality. Below is an easy method to create a basic digital timer that counts up in seconds.

Simple Digital Timer in Java:

This example uses javax.swing.Timer because it’s straightforward and integrates well with the Swing GUI, handling the event dispatch thread correctly.

  • Create a new Java file: Name it SimpleDigitalTimer.java.
  • Add the following code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class SimpleDigitalTimer extends JFrame {
        private int seconds = 0;
        private JLabel timeLabel;
        private Timer timer;
    
        public SimpleDigitalTimer() {
            // Set up the frame
            setTitle("Simple Digital Timer");
            setSize(300, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            
            // Initialize the time label
            timeLabel = new JLabel("Time: 0 s", SwingConstants.CENTER);
            timeLabel.setFont(new Font("Arial", Font.BOLD, 30));
            add(timeLabel, BorderLayout.CENTER);
    
            // Create a timer that updates every 1000 milliseconds (1 second)
            timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    seconds++;
                    timeLabel.setText("Time: " + seconds + " s");
                }
            });
    
            // Create start and stop buttons
            JPanel buttonPanel = new JPanel();
            JButton startButton = new JButton("Start");
            JButton stopButton = new JButton("Stop");
    
            startButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.start();
                }
            });
    
            stopButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.stop();
                }
            });
    
            buttonPanel.add(startButton);
            buttonPanel.add(stopButton);
            add(buttonPanel, BorderLayout.SOUTH);
        }
    
        public static void main(String[] args) {
            // Run the GUI in the Event Dispatch Thread
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    SimpleDigitalTimer timer = new SimpleDigitalTimer();
                    timer.setVisible(true);
                }
            });
        }
    }
    

    Key Points:

  • GUI Setup: The timer uses JFrame to display a window, with a central JLabel showing the time in seconds.
  • Timer: javax.swing.Timer is used to increment the seconds every 1 second (1000 milliseconds).
  • Buttons: Start and Stop buttons are provided to control the timer.
  • Thread Safety: The GUI runs on the Event Dispatch Thread using SwingUtilities.invokeLater.
  • Running the Program:

  • Save the code in a file named SimpleDigitalTimer.java.
  • Compile the program using:
    javac SimpleDigitalTimer.java
    

    Run the program using:

    java SimpleDigitalTimer
    

    This will open a simple GUI with a timer that counts seconds, with options to start and stop the timer.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top