java 定时器启动/停止参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7615248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timer start/stop parameters
提问by Speakr
I've made leaps and bounds in skill and progress since joining this community. You all are a huge help. I'm having trouble with giving a timer that I've implemented certain parameters for when it starts and stops.
自从加入这个社区以来,我在技能和进步方面取得了长足的进步。你们都是一个巨大的帮助。我在提供一个计时器时遇到了麻烦,我已经在它开始和停止时实现了某些参数。
I either get errors saying "the local variable timer may not have been initialized" or I get no errors, but nothing happens. Maybe I have the timer in the wrong place?
我要么收到错误消息,说“局部变量计时器可能尚未初始化”,要么我没有收到错误消息,但没有任何反应。也许我把计时器放错了地方?
If I put timer.start();
in the constructor too everything works fine, but then the timer has to start when the program is initialized. I would really like to have the timer not start until a certain parameter is met. Say, for instance, until the int p1Laps=1;
but if I place timer.start();
into an if-statement in the constructor (i.e. if(p1Laps>=1) { timer.start(); }
the timer doesn't ever start.
如果我也放入timer.start();
构造函数,一切正常,但是定时器必须在程序初始化时启动。我真的很想让计时器在满足某个参数之前不启动。比如说,直到int p1Laps=1;
但是如果我 timer.start();
在构造函数中放入一个 if 语句(即if(p1Laps>=1) { timer.start(); }
计时器永远不会启动。
I've tried placing timer.start();
in various places and have either gotten no response or generated an error about the lack of local variable timer
.
我试过放置timer.start();
在不同的地方,要么没有得到响应,要么产生了关于缺少局部变量的错误timer
。
A second, somewhat related problem I have is the inability to put any parameters in place to call on timer.stop();
without getting the aforementioned "local variable timer may not have been initialized" error. I've left timer.stop();
where I think it needs to be in the code, but it receives that error.
我timer.stop();
遇到的第二个有点相关的问题是无法在没有得到上述“局部变量计时器可能尚未初始化”错误的情况下将任何参数放在适当的位置进行调用。我已经离开timer.stop();
了我认为它需要在代码中的地方,但它收到了那个错误。
So in short, I want to be able to tell the timer to start when a parameter is met, namely when a player has completed a lap. And I want to be able to tell the timer to stop when it reaches a value.
简而言之,我希望能够告诉计时器在满足参数时启动,即当玩家完成一圈时。我希望能够告诉计时器在达到某个值时停止。
Thanks in advance for the great advice I'm sure I'll receive. Note: this is not the whole code, just relevant information.
在此先感谢您的宝贵建议,我相信我会收到。注意:这不是全部代码,只是相关信息。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
public class RacerDoom extends JFrame {
int counter = 0;
int p1Laps = 0;
public RacerDoom() {
//create JFrame
super("Racer Doom Squared");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//set up Timer
final Timer timer=new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(counter>=10) {
timer.stop(); //error here reads "local variable timer may
//not have been initialized"
}
else{
counter++;
}
System.out.println(counter);
}
});
//inner class threads
Move1 m1 = new Move1();
m1.start();
//start timer
if(p1Laps>=1) {
timer.start(); //error here is that timer will not start when
//p1Laps>=1
}
}
private class Move1 extends Thread implements KeyListener {
public void run() {
addKeyListener(this);
while(true) {
try {
repaint();
//collisions
if(p1.intersects(finishtop)&&p1Direction==UP&&p1cross!=true){
p1cross=true;
p1Laps++;
p1Boost++;
counter=0;
System.out.println(p1Laps);
}
if(p1.intersects(finishtop)==false) {
p1cross=false;
}
public static void main (String [] args) {
new RacerDoom();
}
}
回答by Ashwinee K Jha
As you want to start and stop the timer at different places in the code you should make it member variable. This will fix the problem where you are trying to stop the timer inside the action listener.
当您想在代码的不同位置启动和停止计时器时,您应该将其设为成员变量。这将解决您试图在动作侦听器中停止计时器的问题。
The variable p1Laps will not change in the constructor (after you have initialized it to 0) so you need to start the timer where you change the value of plLaps. I am not sure if it is safe to call timer.start() from another thread (Move1). So it may be safer to start timer with SwingUtilities.invokeLater().
变量 p1Laps 在构造函数中不会改变(在你将它初始化为 0 之后),所以你需要启动定时器,在那里你改变 plLaps 的值。我不确定从另一个线程(Move1)调用 timer.start() 是否安全。因此,使用 SwingUtilities.invokeLater() 启动计时器可能更安全。
回答by Hovercraft Full Of Eels
Quick fix:
快速解决:
Rather than
而不是
timer.stop();
Do
做
((Timer)e.getSource()).stop();
The ActionEvent's getSource method will return a reference to the object that calls the actioPerformed method (the Timer), so this should work.
ActionEvent 的 getSource 方法将返回对调用 actionPerformed 方法(Timer)的对象的引用,因此这应该可以工作。
There may be other issues with your code including your background thread without a Thread.sleep(...), your use of KeyListeners rather than Key Binding, your adding a KeyListener in a background thread,...
您的代码可能存在其他问题,包括您的后台线程没有 Thread.sleep(...)、您使用 KeyListeners 而不是 Key Binding、您在后台线程中添加 KeyListener,...