-3

I've been working on this stopwatch application for a decent amount of time now but I've encountered some problems.

Problems

  1. The stopwatch isn't starting properly probably due to the clock logic
  2. The stopwatch isn't displaying the numbers onto my Java program properly

It would be nice if anyone were to help explain the flaws in my program and tell me why it isn't working the way I would like it to be. Much help needed and appreciated so here is my code.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Clock {

    private static void createWindow() {

        // Important
        final int windowWidth = 800; // Window width
        final int windowHeight = 300; // Window height
        // Clock variables
        boolean clockRunning = true; // When clock is running, this will be true
        int milliseconds = 0;
        int seconds = 0;
        int minutes = 0;
        int hours = 0;

        // Create JFrame
        JFrame frame = new JFrame(); // JFrame object

        // Create timer text
        JLabel timer = new JLabel("00:00:00");
        timer.setText("00:00:00");
        timer.setBounds(355, 100, 100, 40); // Button position

        // JButtons
        JButton startTimer = new JButton("Start the timer"); // Start timer button
        JButton stopTimer = new JButton("Stop the timer"); // Stop timer button

        // Event listeners

        startTimer.addActionListener(new ActionListener() // Start timer
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Timer has started");
            }
        });

        stopTimer.addActionListener(new ActionListener() // Stop timer
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Timer has stoped");
            }
        });

        // Clock logic
        if (clockRunning = true) {
            milliseconds = 0;
            seconds++;
        }

        if(milliseconds > 1000) {
            milliseconds=0;
            seconds++;
        }

        if(seconds > 60) {
            milliseconds=0;
            seconds=0;
            minutes++;
        }

        if(minutes > 60) {
            milliseconds=0;
            minutes=0;
            hours++;
        }

        timer.setText(" : " + seconds); // Milliseconds
        timer.setText(" : " + milliseconds); // Seconds
        timer.setText(" : " + minutes); // Minutes
        timer.setText("" + hours); // Hours

        // JButton Settings
        startTimer.setBounds(10, 100, 200, 30); // Button position
        stopTimer.setBounds(570, 100, 200, 30); // Button position

        // Frame Settings
        frame.setSize(windowWidth, windowHeight); // Window size
        frame.setLayout(null); // Frame position

        // Add
        frame.add(startTimer);
        frame.add(stopTimer);
        frame.add(timer);

        // Frame settings
        frame.setVisible(true); // Make frame visible
        frame.setResizable(false); // Disables maximize
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allows the window to be closed
    }

    public static void main(String[] args) {
        createWindow();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Create a Swing `Timer` that increments the no of milliseconds then does the checks of the count and updates the label. Start and stop the `Timer` from the action listeners already defined. – Andrew Thompson Mar 03 '20 at 16:54
  • `if (clockRunning = true)` ← That is not doing what you think it does. booleans are discrete types, so you should never use equality to check them. `if (clockRunning)` is sufficient (and will not risk the error you have made). – VGR Mar 03 '20 at 18:46
  • @Andrew Thompson What do you mean by "no of milliseconds" – Ethan Reed Mar 09 '20 at 20:50
  • By 'no' I meant 'number'. – Andrew Thompson Mar 10 '20 at 03:39

1 Answers1

2

The stopwatch isn't displaying the numbers onto my Java program properly

timer.setText(" : " + seconds); // Milliseconds
timer.setText(" : " + milliseconds); // Seconds
timer.setText(" : " + minutes); // Minutes
timer.setText("" + hours); // Hours

The setText(…) method replaces the existing text.

So the above code is the same as:

timer.setText("" + hours); // Hours

The stopwatch isn't starting properly probably due to the clock logic

For a stop watch you will need to use a Swing Timer so you can update the text at a specified interval.

See: Program freezes during Thread.sleep() and with Timer for the basics of using a Timer.

camickr
  • 321,443
  • 19
  • 166
  • 288