我怎么能停止我的计时器?

时间:2014-04-25 23:00:07

标签: 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;
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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

调用它的。restart()方法来重新启动它。和.stop()来阻止它。

答案 1 :(得分:0)

请查看How to Use Swing Timers上的示例代码。

使用timer.restart()重新启动它,timer.stop()停止它。