2014-05-17 3 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(); 
        } 
       } 
      } 
     } 
    } 
} 

두 번째 부분 UI 스레드에서 Thread.sleep를 호출

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 업데이트가 비활성화됩니다.

자세히 확인하지는 않았지만 카드 4의 도면이 예정되어 있지만 스레드가 잠자기 상태가되면 아직 실행되지 않은 것처럼 보입니다.