I am trying to build a timer in java.
Expected Output:
When you run the program, there's a window with a big "0" in it.
Once the user presses any key from the keyboard, the number increases by 1 every second.
Here's what I have.
import javax.swing.*;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class TimerTest implements KeyListener {
static final int SCREEN_WIDTH = 960;
static final int SCREEN_HEIGHT = 540;
static boolean timerStarted;
static int keyPressedNum; //amount of times the user pressed any key.
static JLabel label;
static int currentTime;
public static void main(String[] args) {
JFrame frame = new JFrame("TimerTest");
JPanel panel = new JPanel();
label = new JLabel();
label.setFont(new Font("Arial", Font.PLAIN, 256));
label.setText("0");
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.CENTER);
frame.addKeyListener(new TimerTest());
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
while (true) {
while (TimerTest.timerStarted == false) {
if (TimerTest.timerStarted == true) {break;}
}
try {Thread.sleep(1000);}
catch (InterruptedException ex) {ex.printStackTrace();}
currentTime++;
label.setText(String.valueOf(currentTime));
}
}
public void keyPressed(KeyEvent e) {
System.out.println("Keypress indicated.");
TimerTest.timerStarted = true;
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
When I press any key, the program sets timerStarted as true.
Because timerStarted is true, I was expecting this part:
while (TimerTest.timerStarted == false) {
if (TimerTest.timerStarted == true) {break;}
}
to break out of the loop.
Instead when I run the program and press any key, the command line just prints: Keypress indicated., and it doesn't break out of that loop.
Now here's the weird part:
I tried adding some code to the while block, like this.
while (TimerTest.timerStarted == false) {
System.out.println("currently stuck here...");
//(I simply added a println code)
if (TimerTest.timerStarted == true) {break;}
}
Surprisingly when I do this, the program works just like how I wanted. It breaks out of that loop and the timer runs.
This confuses me. Adding a System.out.println(); fixes the issue...?
Why does not having System.out.println() makes me unable to break out of the loop?
Any explanations would be appreciated =)
TimerTest.timerStarted == truewould logically never fall underTimerTest.timerStarted == false(unless something infinitesimally unimaginably precise makes a predictable change between the twoifexecutions)TimerTest.timerStarted == true. However, both cases do not work.TimerTest.timerStarted. Is it being changed outside?timerStarted= true. I used the while loop to "wait" untiltimerStartedis equal to truebreakwill break out of the nearest loop, not all loops. You have awhilein yourwhile, so when you break out of the innerwhile, you should still be in the outerwhile.