2016-08-03 58 views
-1

我用java做了一個遊戲,但它只運行了一輪。我希望它能夠根據在一個撥號盒中按下的數字運行一定的時間。我認爲答案是在一場比賽結束後運行該方法,但我不確定如何執行此操作。如何讓方法運行一定的時間

創建JFrame的和metthod

public Rockgame() { 

    // JFrame 
    p = new ImagePanel(Toolkit.getDefaultToolkit().getImage("space.jpg")); 
    f = new JFrame("SpaceMiners"); 

    f.setSize(700, 500); 
    f.setResizable(false); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

其確定的倍量運行的按鈕:

Object[] options = { "Three", "Five", "Ten" }; 
    no_of_games = (int) JOptionPane.showOptionDialog(f, 
      "Would you like to play best of:", "Rounds" + "", 
      JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, 
      null, options, options[2]); 

if (no_of_games == 0) { 
     no_of_games = no_of_games + 3; 
    } 
    if (no_of_games == 1) { 
     no_of_games = no_of_games + 4; 
    } 
    if (no_of_games == 2) { 
     no_of_games = no_of_games + 8; 
    } 

創建網格和的MouseListener

p.setLayout(new GridLayout(10, 10)); 
    f.getContentPane().add(p, BorderLayout.CENTER); 

    for (int x = 0; x < 10; x++) { 

     for (int y = 0; y < 10; y++) { 

      playingGrid[x][y] = new JLabel(new ImageIcon("rock.png")); 
      p.add(playingGrid[x][y]); 

      playingGrid[x][y].addMouseListener(new Rockbreaker()); 

     } 
    } 

} 

public class Rockbreaker implements MouseListener { 
    // manbitesdog6 
    public void mouseClicked(MouseEvent e) { 
     // sets all columns greater than one clicked to invisible 

      if (e.getSource() == playingGrid[0][0]){ 
       for(int u =0;u<10;u++){ 
       for(int y =0;y<10;y++){ 
       Rockgame(); 

       } 
      } 
      } 

      for (int x = 0; x < 10; x++) { 
       for (int y = 0; y < 10; y++) { 
        if (playingGrid[x][y] == e.getSource()) { 
         for (int k = 0; k < 10; k++) { 
          for (int i = 0; i < 10; i++) { 
           if ((i >= x) && (k >= y)) { 
            playingGrid[i][k].setVisible(false); 
           } 
          } 
         } 

        } 
       } 
      } 
+0

爲(其中你想3,5或10)的函數=圓形或者是所有的地方的代碼? TL; DR我可以稱之爲startRound() –

+0

他們點擊遊戲的部分是在mouselistener中,我不認爲它是 – michael

+0

RockGame()函數中有什麼?在rockGame()方法中, –

回答

0

嘗試使用for() -循環。

for(int i = 0; i < no_of_games; i++) { 
    /* 
     * Start game 
     * Reset grid/panel 
     * ... 
     */ 
} 

而且,看看這個:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

+0

我試過但它只是說for循環什麼都不做。 – michael

+0

是的,因爲在我的示例代碼中循環中只有一條註釋。 你當然應該自己實現你的代碼,我只是給你一個for()循環的代碼,它目前什麼都不做。 我不會爲你實施 - 現在輪到你了...... –