JAVA:启动计时器,0秒停止计时器,重启计时器

时间:2015-06-14 15:48:00

标签: java timer countdown timertask

私有代码位于Jbutton中,在单击鼠标时启动计时器。当经过的时间等于原始时间时,底部附近的if语句不会停止计时器,但不会。我该如何解决?此外,当我按下按钮时,计时器以增加的速度加速。

import java.awt.*;
import java.util.*;


public class refractiveIndex extends javax.swing.JFrame {

public static int time = 10;
public static int elapsedTime = 0;

private void nextQActionPerformed(java.awt.event.ActionEvent evt) { 

time = 10;
elapsedTime = 0;                                     

    final Timer timer = new Timer();
    TimerTask task = new TimerTask()
    {
        public void run()
                {
                    elapsedTime++;//amount of time passed
                    timeLeft.setText("" + (time - elapsedTime));//int 'time' = 0 (this is time left)
                }
    };

    if(time - elapsedTime == 0)
    {
        timer.cancel();//timer stops after 5 secs
        score1 = 0;//resets score
        question.setText("GAME OVER");
    }

    timer.scheduleAtFixedRate(task, 500, 500);//.5 second delay, rate of .5 second

1 个答案:

答案 0 :(得分:1)

您可以调整自定义import csv with open("output.csv", "wb") as f: writer = csv.writer(f) writer.writerows([[u.encode('utf-8') for u in row] for row in temp])

TimerTask

另见this question