gui计时器不停止

4bbkushb  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(277)

我的gui计时器有问题。当玩家点击矿区的按钮时,我启动计时器,当游戏结束时,我停止计时器。
我第一次测试我的游戏,计时器开始和结束正常。然而,如果我尝试玩游戏的第二轮,我的计时器不会停止。

public class Minesweeper7 extends JFrame implements ActionListener {

static public boolean revealed [][];

public static CountTimer ct;

public Minesweeper7 (int row, int column) {

ct = new CountTimer ();

}

private void setTimerText (String sTime) {

    timeLabel.setText (sTime);

  }

public void actionPerformed(ActionEvent event){

String coords = event.getActionCommand();
        String[] squarePressed = coords.split(",");

        x_pressed = Integer.parseInt(squarePressed[0]);
        y_pressed = Integer.parseInt(squarePressed[1]);

        System.out.println("The button you have pressed is " + x_pressed + ", " + y_pressed);

        if (ct.timer.isRunning() == false){
          ct.start ();
          System.out.println ("timer is running");
        }

        reveal (row,column,x_pressed,y_pressed,revealed,mines,revealed_number);

        gameEnd (row, column, revealed, mines, countMines, counter, end);

        System.out.println("There are " + countMines + " .");

        System.out.println("");

        for (int r = 0;r<row;r++){
          for (int c = 0;c<column;c++){

            label[r][c].setText(String.valueOf(revealed_number[r][c]));

          }
        }

}

public void reveal () {
//a recursive method that reveals my buttons
}

public static void main (String args []) {

    java.awt.EventQueue.invokeLater (new Runnable () {
      public void run () {
        new Minesweeper7 (10, 10); 
      }
    });

  }

public void gameEnd (int row, int column, boolean revealed [][], boolean mines [][], int countMines, int counter, boolean end) {

    for (int r = 0; r < row; r++) {
      for (int c = 0; c < column; c++) {
        if (mines [r][c] == false && revealed [r][c] == true) {
          counter++;
        }

        else if (mines [r][c] == true && revealed [r][c] == true) {
          System.out.println ("Sorry, you lost because you clicked on a mine.");
          end = true;
          break;
        }
      }
    }

    if (counter == (row*column-countMines)) {
      System.out.println ("Congratulations! You won the game!");
      end = true;
      instruction.setText("Congratulations! You won the game!");
    }

    if (end == true) {
      ct.stop ();
    }

  }

public class CountTimer implements ActionListener {
    public static final int ONE_SECOND = 1000;
    public int count = 0;
    public boolean isTimerActive = false;
    public Timer timer = new Timer (ONE_SECOND, this);

    public CountTimer () {
      count = 0;
      setTimerText (TimeFormat (count));
    }

    public void actionPerformed (ActionEvent e) {
      if (isTimerActive) {
        count++;
        setTimerText (TimeFormat (count));
      }
    }

    public void start () {
      count = 0;
      isTimerActive = true;
      timer.start ();
    }

    public void stop () {
      timer.stop ();
    }

    public void reset () {
      count = 0;
      isTimerActive = true;
      timer.restart ();
    }
  }

}
sxpgvts3

sxpgvts31#

如果你开始一个新的游戏,你可以做一个新的计时器,而不是开始和停止。

if (game end) {

    timer = new Timer();
    timer.start();
}

相关问题