2011-07-12 2 views
1

私はシンプルなビデオポーカープログラムを作成しています。今は、ユーザーが保持したいカードを指定した後に実行されたアクションを実行しています。抽選後の新しいカード。私はアクションを持っていますが、私はすべての置換の間に遅れてカードを1つずつ置き換えたいのですが、私が以下に示すコードでは、置き換えなければならないカードの数を500ミリ秒掛けてスリープします。一度に1枚ずつカードを交換するのではなく、一度にカードを交換してください。どんな助けでも大歓迎です!Thread.sleep()に関する問題

Action drawAction = new AbstractAction() { 
     public void actionPerformed(ActionEvent e) { 
      int deckPos = 5; 

      if((holdValFirst.getText()).equals("HELD")){} 
      else{     
       holdFirst.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif")); 
       deckPos++; 
       try 
       { 
        Thread.sleep(500);    
       }catch (InterruptedException ie){ 
        System.out.println(ie.getMessage()); 
       } 
      } 
      if((holdValSecond.getText()).equals("HELD")){} 
      else{     
       holdSecond.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif")); 
       deckPos++; 
       try 
       { 
        Thread.sleep(500);    
       }catch (InterruptedException ie){ 
        System.out.println(ie.getMessage()); 
       }    
      } 
      if((holdValThird.getText()).equals("HELD")){} 
      else{ 
       holdThird.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif")); 
       deckPos++; 
       try 
       { 
        Thread.sleep(500);    
       }catch (InterruptedException ie){ 
        System.out.println(ie.getMessage()); 
       }     
      } 
      if((holdValFourth.getText()).equals("HELD")){} 
      else{     
       holdFourth.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif")); 
       deckPos++; 
       try 
       { 
        Thread.sleep(500);    
       }catch (InterruptedException ie){ 
        System.out.println(ie.getMessage()); 
       }    
      } 
      if((holdValFifth.getText()).equals("HELD")){} 
      else{          
       holdFifth.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif")); 
       deckPos++;         
      }    
     } 
    }; 
+2

コードに重複があります。あなたはおそらく、あなたのすべてのifの "else"部分のためのメソッドを書く必要があります... – phtrivier

+0

[java thread.sleep puts swing ui too sleep]の可能な複製(http://stackoverflow.com/questions/4215968/java-スレッドスリープ - スイング - スリープ - スリープ - あまりにも) –

答えて

8

イベントディスパッチスレッド(EDT)内でスリープ状態になると、GUIがフリーズします。すべての長時間の作業はEDT外で行う必要があり、すべてのスイング操作はEDTで行う必要があります。

別のスレッドでスリープするにはSwingWorkerを使用し、500msごとにいくつかの進捗状況を公開する必要があります。または、を使用すると、500ミリ秒ごとにイベントが発生します。