Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
248 views
Welcome To Ask or Share your Answers For Others

1 Answer

Its just pseudo code to see how timer can be started and stopped.

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

public class TimerOnJLabel extends JFrame {

    private static final long serialVersionUID = 1L;        
    long start = System.currentTimeMillis();
    long elapsedTimeMillis;
    int sec = 5;
    Timer timer;

    public TimerOnJLabel() {
        super("TooltipInSwing");
        setSize(400, 300);
        getContentPane().setLayout(new FlowLayout());
        final JLabel b1;
        final JRadioButton jrb = new JRadioButton();
        b1 = new JLabel("Simple tooltip 1");

        ActionListener timerTask = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                elapsedTimeMillis = System.currentTimeMillis();
                b1.setText("Timer : " + (elapsedTimeMillis-start)/1000+" ::::: " +sec);
                System.out.println("Timer working: " + sec);
                if(--sec == 0){
                    timer.stop();
                    System.out.println("Timer Stopped");
                }
            }
        };
        timer = new Timer(1000, timerTask);
        System.out.println("Timer Started");
        timer.start();

        getContentPane().add(b1);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);          
        setVisible(true);
    }

    public static void main(String args[]){
        new TimerOnJLabel();
    }
}

I hope this help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...