Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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_User Interface_Timer_Jframe_Actionlistener - Fatal编程技术网

Java 我该如何停止计时器?

Java 我该如何停止计时器?,java,user-interface,timer,jframe,actionlistener,Java,User Interface,Timer,Jframe,Actionlistener,因此,我为我创建的一个游戏创建了一个计时器类,我已经设置了启动按钮动作来启动计时器,但是我有点困惑如何停止计时器并将其重置为0。如果有人能解决我的问题,那就太好了 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyTimer extends Panel { private JLabel timeDisplay; private JButton resetButton; priva

因此,我为我创建的一个游戏创建了一个计时器类,我已经设置了启动按钮动作来启动计时器,但是我有点困惑如何停止计时器并将其重置为0。如果有人能解决我的问题,那就太好了

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

public class MyTimer extends Panel {


private JLabel timeDisplay;
private JButton resetButton;
private JButton startButton;
private JButton stopButton;
Timer timer;

public MyTimer(){

    MyTimer timer;
    startButton = new JButton("Start Timer");
    stopButton = new JButton("Stop Timer");
    timeDisplay = new JLabel("...Waiting...");
    resetButton = new JButton("Reset Timer");

    this.add(resetButton);
    this.add(startButton);
    this.add(stopButton);
    this.add(timeDisplay);

    event e = new event();
    startButton.addActionListener(e);

    event1 c = new event1();
    stopButton.addActionListener(c);

    event2 d = new event2();
    resetButton.addActionListener(d);

}

public class event implements ActionListener{
    public void actionPerformed(ActionEvent e){
        int count = 0;
        timeDisplay.setText("Elapsed Time in Seconds: " + count);

        TimeClass tc = new TimeClass(count);
        timer = new Timer(1000, tc);
        timer.start();
    }
}

public class TimeClass implements ActionListener{
    int counter;

    public TimeClass(int counter){

        this.counter = counter;

    }

    public void actionPerformed(ActionEvent e){
        counter++;

        timeDisplay.setText("Elapsed Time in Seconds: " + counter);

    }
}

class event1 implements ActionListener{
    public void actionPerformed (ActionEvent c){
        timer.stop();
    }
}

class event2 implements ActionListener{
    public void actionPerformed (ActionEvent d){
        timer.restart();
    }
}
}
编辑

好的,我有停止按钮做它需要完美完成的事情,但是重置按钮有问题。它将计时器暂停一秒钟,然后从暂停的位置开始计数,例如:它将在4秒时暂停一秒钟,然后继续到5、6等

有什么建议吗

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

public class MyTimer extends Panel {


    private JLabel timeDisplay;
    private JButton resetButton;
    private JButton startButton;
    private JButton stopButton;
    Timer timer;

    public MyTimer(){

        MyTimer timer;
        startButton = new JButton("Start Timer");
        stopButton = new JButton("Stop Timer");
        timeDisplay = new JLabel("...Waiting...");
        resetButton = new JButton("Reset Timer");

        this.add(resetButton);
        this.add(startButton);
        this.add(stopButton);
        this.add(timeDisplay);

        event e = new event();
        startButton.addActionListener(e);

        event1 c = new event1();
        stopButton.addActionListener(c);

        event2 d = new event2();
        resetButton.addActionListener(d);

    }

    public class event implements ActionListener{
        public void actionPerformed(ActionEvent e){
            int count = 0;
            timeDisplay.setText("Elapsed Time in Seconds: " + count);

            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000, tc);
            timer.start();
        }
    }

    public class TimeClass implements ActionListener{
        int counter;

        public TimeClass(int counter){

            this.counter = counter;

        }

        public void actionPerformed(ActionEvent e){
            counter++;

            timeDisplay.setText("Elapsed Time in Seconds: " + counter);

        }
    }

    class event1 implements ActionListener{
        public void actionPerformed (ActionEvent c){
            timer.stop();
        }
    }

    class event2 implements ActionListener{
        public void actionPerformed (ActionEvent d){
            timer.restart();
        }
    }
}

称其为。方法来。。重新启动它。和。停止以停止它。

请查看上的示例代码

使用timer.restart重新启动,使用timer.stop停止