摇摆计时器不停止 [英] Swing timer not stopping
问题描述
我正在使用摆动计时器在Netbeans中制作一个倒数时钟:
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;
}
}
我只是使用System.out.print来测试程序,它不是真实程序的一部分.我调用了stop()方法,但计时器继续计数.另外,我通过 displayTimer = new javax.swing.Timer(10000,listener);
创建了一个新计时器,但是它的计数速度是它的两倍.有人可以帮忙吗?
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?
这是我的计时器(属于SSCCE):
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;
}
}
}
它无法正常工作,因为没有显示秒,但是它确实显示了20次,这正是我想要的.这只是在自己的应用程序中,在我的真实程序中,更容易发现问题.
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.
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();
}
}
我希望有帮助.
这篇关于摇摆计时器不停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!