I'm using the swing Timer to make a countdown clock in Netbeans:
public void startTimer() {
System.out.println(right + "value");
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("action");
timerLabel.setText("" + seconds);
--seconds;
System.out.println(seconds);
if (seconds == -1 && seconds < 0) {
System.out.print("zero");
//displayTimer.stop();
wrong();
dispose();
}
}
};
displayTimer = new Timer(1000, listener);
displayTimer.setInitialDelay(100);
displayTimer.start();
if (right == null) {
System.out.println("null");
} else if (right == true) {
System.out.println("truehere");
displayTimer.stop();
right = null;
seconds = 20;
displayTimer.setDelay(10000);
displayTimer.setInitialDelay(100);
displayTimer.start();
} else if (right == false) {
System.out.print("wrong");
//displayTimer.stop();
seconds = 20;
}
}
I just use System.out.print to test the program, it's not a part of the real program.
I call the stop() method but the timer continues to count. Also, I create a new timer by displayTimer = new javax.swing.Timer(10000, listener);
but it counts twice as fast. Can anyone help?
EDIT:
Here is my timer (sort of SSCCE):
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;
public class JavaApplication8 {
public static void startTimer() {
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
int seconds = 20;
seconds--;
System.out.println(seconds);
if (seconds == -1 && seconds < 0) {
System.out.print("zero");
}
}
};
Timer displayTimer = new Timer(1000, listener);
displayTimer.setInitialDelay(100);
displayTimer.start();
}
public static void main(String[] args) {
System.out.println("Type win to win!");
startTimer();
String read;
Boolean right;
int seconds;
Scanner scanIn = new Scanner(System.in);
read = scanIn.nextLine();
if (read.equals("win")){
right = true;
}
else{
right = false;
}
if (right == true) {
System.out.println("correct");
//displayTimer.stop();
right = null;
seconds = 20;
//displayTimer.setDelay(10000);
//displayTimer.setInitialDelay(100);
//displayTimer.start();
} else if (right == false) {
System.out.print("incorrect");
//displayTimer.stop();
seconds = 20;
right = null;
}
}
}
it doesn't work right in that the seconds don't show up, but it does show 20 times which is what I want. This is just in its own application, in my real program it is easier to see the problem.
I've noticed that the first time the game runs it works fine. Then I click play again (resets the whole game) and it goes twice as fast. Maybe I'm not resetting something correctly? Here is my reset code:
// Reset Everything
PlayFrame.seconds = 20;
PlayFrame.winnings = 0;
PlayFrame.right = false;
//PlayFrame.displayTimer.stop();
PlayFrame.questionLabel.setText(null);
PlayFrame.count = 0;
WelcomeFrame WFrame = new WelcomeFrame();
WFrame.setVisible(true);
setVisible(false);
PlayFrame P = new PlayFrame();
P.dispose();
if (PlayFrame.seconds == -1 && PlayFrame.seconds < 0){
PlayFrame.displayTimer.stop();
}
}
Its just pseudo code to see how timer can be started and stopped.
I hope this help.