2016-05-09 6 views
1

GUIタイマーに問題があります。プレイヤーが地雷欄のボタンをクリックするとタイマーが始まり、ゲームが終了するとタイマーが止まります。GUIタイマーが停止しない

初めて試合をテストするとき、タイマーは通常どおり開始され、終了します。しかし、試合の第2ラウンドをプレイしようとすると、タイマーは止まらない。

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

} 
+0

他のラウンドを許可するコードは何ですか? – DarkV1

答えて

0

新しいゲームを開始した場合、開始と停止の代わりに新しいタイマーを作成するだけで済みます。

if (game end) { 

    timer = new Timer(); 
    timer.start(); 
} 
関連する問題