Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 定时器启动/停止参数_Java_Timer_If Statement_Conditional - Fatal编程技术网

Java 定时器启动/停止参数

Java 定时器启动/停止参数,java,timer,if-statement,conditional,Java,Timer,If Statement,Conditional,自从加入这个社区以来,我的技能和进步都有了飞跃。你们都帮了大忙。我很难给出一个计时器,我已经为它的启动和停止实现了某些参数 我要么收到错误,说“局部变量计时器可能尚未初始化”,要么没有收到错误,但什么也没有发生。也许我把计时器放错地方了 如果我放入timer.start()

自从加入这个社区以来,我的技能和进步都有了飞跃。你们都帮了大忙。我很难给出一个计时器,我已经为它的启动和停止实现了某些参数

我要么收到错误,说“局部变量计时器可能尚未初始化”,要么没有收到错误,但什么也没有发生。也许我把计时器放错地方了

如果我放入
timer.start()intp1laps=1timer.start()编码到构造函数中的if语句中(即
if(p1Laps>=1){timer.start();}
计时器从未启动

我尝试将
timer.start();
放置在不同的位置,但要么没有得到响应,要么生成了一个关于缺少局部变量
timer
的错误

第二个有点相关的问题是,如果没有得到前面提到的“局部变量计时器可能尚未初始化”错误,就无法将调用
timer.stop();
的任何参数放置到位。我将
timer.stop();
放在代码中需要的位置,但它会收到该错误

简而言之,我希望能够告诉计时器在满足某个参数时开始,即当一名球员完成一圈时,我希望能够告诉计时器在达到某个值时停止

提前感谢您的建议,我相信我会收到。注意:这不是全部代码,只是相关信息

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();
    }
}
快速修复:

而不是

timer.stop();

ActionEvent的getSource方法将返回对调用ActionFormed方法(计时器)的对象的引用,因此这应该可以工作


您的代码可能还有其他问题,包括没有线程的后台线程。sleep(…),使用KeyListener而不是键绑定,在后台线程中添加KeyListener,…

如果要在代码中的不同位置启动和停止计时器,则应将其设置为成员变量。这将解决试图在操作侦听器中停止计时器的问题


变量p1Laps将不会在构造函数中更改(在您将其初始化为0后),因此您需要在更改plLaps值的位置启动计时器。我不确定从另一个线程(Move1)调用timer.start()是否安全。因此,使用SwingUtilities启动计时器可能更安全。invokeLater().

对不起,我的背景线程确实有一个
线程。sleep(…);
我只是忘了包含它。我需要研究
SwingUtilities.invokeLater()
,正如我在这个网站上多次提到的,但我对它完全不熟悉。嗨,我能问一些问题吗?在java中,如果我们将计时器对象分配给null值(timer=null;)在不调用timer.stop()之前,计时器是否会停止向侦听器触发?否,它不会停止计时器,因为计时器引用仍然存在于内部队列或要触发的计时器中(由内部线程维护)。
((Timer)e.getSource()).stop();