2014-05-17 42 views
-1

我想用Java創建甲板遊戲。我想一個接一個地顯示四張牌,然後等待幾秒鐘並顯示另外四張牌,依此類推。我創建了下面的代碼,但卡號#4根本不顯示。例如,它顯示3張卡片,然後等待幾秒鐘,然後顯示其他3張卡片。代碼甲板牌的時間延遲

第一部分:

public void addFirstCenterLabel() { 
    // centerPanelLabel.add(labelFirstCenter); 
    // centerPanel.add(centerPanelLabel); 
    // down table 
    centerPanel.setLayout(new GridBagLayout()); 
    c.gridx = 1; 
    c.gridy = 3; 
    centerPanel.add(labelThirdCenter, c); 
    add(centerPanel); 
} 

public void addSecondCenterLabel() { 
    // centerPanelLabel.add(labelSecondCenter); 
    // centerPanel.add(centerPanelLabel); 
    // right table 
    c.gridx = 2; 
    c.gridy = 1; 
    centerPanel.add(labelForthCenter, c); 
    add(centerPanel); 
} 

public void addThirdCenterLabel() { 
    // centerPanelLabel.add(labelThirdCenter); 
    // centerPanel.add(centerPanelLabel); 
    // top label 
    c.gridx = 1; 
    c.gridy = 0; 
    centerPanel.add(labelSecondCenter, c); 
    add(centerPanel); 
} 

public void addForthCenterLabel() { 
    // centerPanelLabel.add(labelForthCenter); 
    // centerPanel.add(centerPanelLabel); 

    // left table 
    c.gridx = 0; 
    c.gridy = 1; 
    centerPanel.add(labelFirstCenter, c); 
    add(centerPanel); 

    // to count the round of the game. each four cards count as 1. 
    for (int j = 0 ; j <= 7 ; j++) { 
     if (rounds[j] == 0) { 
      rounds[j] = 1; 
      break; 
     } 
    } 
} 

的代碼第三部分::

class ButtonListener implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 
     int hideCount = 0; 
     addFirstCenterLabel(); 
     addSecondCenterLabel(); 
     addThirdCenterLabel(); 
     addForthCenterLabel(); 
     for (int i = 0 ; i < 32 ; i++) { 
      if (button[i] == e.getSource()) { 
       if (button[i] == button[0] || button[i] == button[1] || 
        button[i] == button[2] || button[i] == button[3] || 
        button[i] == button[4] || button[i] == button[20] || 
        button[i] == button[21] || button[i] == button[22]) { 
        southPanelbutton.remove(button[i]); 
        southPanelbutton.validate(); 
        southPanelbutton.repaint(); 

        labelThirdCenter.setIcon(showCard[i]); 

       } 
      } 
      if (button[i] == e.getSource()) { 
       if (button[i] == button[5] || button[i] == button[6] || 
        button[i] == button[7] || button[i] == button[8] || 
        button[i] == button[9] || button[i] == button[23] || 
        button[i] == button[24] || button[i] == button[25]) { 
        eastPanelbutton.remove(button[i]); 
        eastPanelbutton.validate(); 
        eastPanelbutton.repaint(); 

        labelForthCenter.setIcon(showCard[i]); 
       } 
      } 
      if (button[i] == e.getSource()) { 
       if (button[i] == button[10] || button[i] == button[11] || 
        button[i] == button[12] || button[i] == button[13] || 
        button[i] == button[14] || button[i] == button[26] || 
        button[i] == button[27] || button[i] == button[28]) { 
        northPanelbutton.remove(button[i]); 
        northPanelbutton.validate(); 
        northPanelbutton.repaint(); 

        labelSecondCenter.setIcon(showCard[i]); 
       } 
      } 
      if (button[i] == e.getSource()) { 
       if (button[i] == button[15] || button[i] == button[16] || 
        button[i] == button[17] || button[i] == button[18] || 
        button[i] == button[19] || button[i] == button[29] || 
        button[i] == button[30] || button[i] == button[31]) { 
        westPanelbutton.remove(button[i]); 
        westPanelbutton.validate(); 
        westPanelbutton.repaint(); 
        labelFirstCenter.setIcon(showCard[i]); 
        hideCount++; 
       } 
      } 
      if (button[i] == e.getSource()) { 
       if (button[i] == button[15] || button[i] == button[16] || 
        button[i] == button[17] || button[i] == button[18] || 
        button[i] == button[19] || button[i] == button[29] || 
        button[i] == button[30] || button[i] == button[31]) { 
        hideCount++; 
        timeDelay(); 
        if (hideCount == 1 || hideCount == 2 || hideCount == 3 || 
         hideCount == 4 || hideCount == 5 || hideCount == 6 || 
         hideCount == 7 || hideCount == 8) { 
         hideCards(); 
        } 
       } 
      } 
     } 
    } 
} 

代碼的第二部分

public void hideCards() { 
    for (int j = 0 ; j <= 7 ; j++) { 
     // System.out.println(rounds[j]); 
     // if the round ==1, make the four cards unvisible 
     if (rounds[j] == 1) { 
      labelThirdCenter.setIcon(emptyCard[0]); 
      labelForthCenter.setIcon(emptyCard[0]); 
      labelSecondCenter.setIcon(emptyCard[0]); 
      labelFirstCenter.setIcon(emptyCard[0]); 
     } 
    } 
} 

public void timeDelay() { 
    try { 
     Thread.sleep(2500); // one second 
    } 
    catch (InterruptedException e) { 
     throw new RuntimeException("Don't know how to handle this", e); 
    } 
} 
+0

你知道如何使用調試器,不是嗎? – meriton

回答

0

在UI線程調用Thread.sleep是不是一個好理念。它將禁用線程正在休眠時的所有UI更新。

我沒有詳細檢查,但看起來卡4的繪圖是預定的,但尚未在線程進入睡眠狀態時執行。