2017-05-04 56 views
0

我正在構建一個Java Swing記憶遊戲。這個想法是點擊第一張牌,它將圖像從baseImage更改爲另一張圖像。當點擊第二張卡片時,它應該做同樣的事情,等待幾秒鐘,然後如果它們不匹配,則將所有事件重置回基地,或者如果它們一起翻轉,則將它們重置爲基地。爲什麼我的第一個CardClicked.setDisabledIcon(img)能夠工作,但我的第二個CardClicked.setDisabledIcon(img)不起作用?

現在,如果它們不匹配,第二張卡永不改變圖像。以下是相關的代碼。應該指出的是,如果他們確實匹配,那麼圖像就會顯示出來,並且在世界上都是正確的。

public void cardClickHandler(ActionEvent ae) { 
    JButton card = new JButton(); 
    card = (JButton) ae.getSource(); 

    // disable the card that was clicked 
    card.setEnabled(false); 

    // behavior for first card clicked 
    if (clickCounter == 0) { 
     firstCardClicked = card; 
     img = new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg"); 
     firstCardClicked.setDisabledIcon(img); 
     System.out.println("Button " + firstCardClicked.getName() + " clicked!"); 

     clickCounter++; 
    } 
    // behavior for second card clicked 
    else { 
     secondCardClicked = card; 
     img = new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg"); 
     secondCardClicked.setDisabledIcon(img); 
     System.out.println("Button " + secondCardClicked.getName() + " clicked!"); 

     clickCounter--; 
    } 

    // behavior if two cards have been clicked and they match 
    if (firstCardClicked.getName().equals(secondCardClicked.getName()) && clickCounter == 0) { 
     // player turn control and scoring 
     if (p1Turn) { 
      p1NumScore++; 
      p1Score.setText(Integer.toString(p1NumScore)); 
      scorePanel.revalidate(); 
      System.out.println("Good job Mike, got a pair!"); 
      p1Turn = !p1Turn; 
     } 
     else { 
      p2NumScore++; 
      p2Score.setText(Integer.toString(p2NumScore)); 
      scorePanel.revalidate(); 
      System.out.println("Good job Peanut, got a pair!"); 
      p1Turn = !p1Turn; 
     } 
    } 
    // behavior if two cards have been clicked and they do not match 
    else if (!(firstCardClicked.getName().equals(secondCardClicked.getName())) && clickCounter == 0) { 
     // keep cards flipped for a few seconds 
     try { 
      System.out.println("Before Sleep");   // testing 
      Thread.sleep(2000); 
      System.out.println("After Sleep");   // testing 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // enable the cards and reset images 
     firstCardClicked.setEnabled(true); 
     firstCardClicked.setIcon(baseImage); 
     secondCardClicked.setEnabled(true); 
     secondCardClicked.setIcon(baseImage); 

     // change turns 
     p1Turn = !p1Turn; 
     System.out.println("Keep Playing"); 
    } 
} 
+0

不要在事件處理中使用'Thread.sleep(int)'。它阻止了GUI。 –

+0

請同時提供一個可運行的簡短示例([MVCE](http://stackoverflow.com/help/mcve))。 –

+0

@SergiyMedvynskyy作爲一個初學者,我試圖遠離'Thread.sleep(int)',因爲它與Swing有關的問題。不幸的是,我無法想出正確實現Swing Timer的方法,並且它運行得很好。我會盡力在明天把這個分解成MVCE。欣賞一切。 –

回答

0

看起來好像有幾個問題在同一時間工作。 Thread.sleep().setEnabled(true)命令一起造成嚴重破壞。對象的狀態有問題,所以我需要在代碼結束時清除它們。有一些.setDisabledIcon()方法的已知問題,許多帖子在這裏說需要一個.setIcon()在它之前。這是與使用的javax.swing.Timer一起使用的固定代碼。

public void cardClickHandler(ActionEvent ae) 
{ 
    // method variables 
    JButton card = (JButton) ae.getSource(); 
    boolean clearState = false; 


    // behavior for first card clicked 
    if (isFirstCard) 
    { 
     firstCardClicked = card; 

     // a known issue with "setDisabledIcon()" required the "setIcon()" method 
     firstCardClicked.setIcon(new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg")); 
     firstCardClicked.setDisabledIcon(new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg")); 

     // disable the flipped card 
     firstCardClicked.setEnabled(false); 

     // indicate the next card clicked is the second card 
     isFirstCard = false; 
    } 
    // behavior for second card clicked 
    else 
    { 
     secondCardClicked = card; 

     // a known issue with "setDisabledIcon()" required the "setIcon()" method 
     secondCardClicked.setIcon(new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg")); 
     secondCardClicked.setDisabledIcon(new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg")); 

     // disable the flipped card 
     secondCardClicked.setEnabled(false); 

     // indicate the next card clicked is the first card again 
     isFirstCard = true; 

     // indicate to the system both cards have been clicked and can clear objects 
     clearState = true; 
    } 

    // behavior if two cards have been clicked and they match 
    if (isFirstCard && firstCardClicked.getName().equals(secondCardClicked.getName())) 
    { 
     // player turn control and scoring 
     if (p1Turn) 
     { 
      p1NumScore++; 
      p1Score.setText(Integer.toString(p1NumScore)); 
      scorePanel.revalidate(); 
      p1Turn = !p1Turn; 
     } 
     else 
     { 
      p2NumScore++; 
      p2Score.setText(Integer.toString(p2NumScore)); 
      scorePanel.revalidate(); 
      p1Turn = !p1Turn; 
     } 
    } 
    // behavior if two cards have been clicked and they do not match 
    else if (isFirstCard && !(firstCardClicked.getName().equals(secondCardClicked.getName()))) 
    { 
     // enable the cards and reset images 
     firstCardClicked.setIcon(BASE_IMAGE); 
     secondCardClicked.setIcon(BASE_IMAGE); 

     // change turns 
     p1Turn = !p1Turn; 
    } 
    if (clearState) 
    { 
     javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 

         firstCardClicked.setEnabled(true); 
         secondCardClicked.setEnabled(true); 
         isFirstCard = true; 
         firstCardClicked = null; 
         secondCardClicked = null; 

      } 
     }); 

    // Normally the timer's actionPerformed method executes repeatedly. Tell it only to execute once. 
    timer.setRepeats(false); 

    // Start the timer. 
    timer.start(); 
    } 
} 
相關問題