简体   繁体   中英

Why does not Thread.sleep() work in Action Performed?

public void actionPerformed(ActionEvent e) {
        String sp1="Player 1's turn. ";
        String sp2="Player 2's turn. ";
          System.out.println("Mouse entered for rating " + index);  //helps me track the cards
           ori=new ImageIcon(getClass().getResource(index+1+".png")); //set the image to the card

        ori.setDescription("ori");  //It's weird that I cannot directly flip the card, so I used this method to flip the cards.
        tail.setDescription("tail");//http://stackoverflow.com/questions/13557561/the-method-about-imageicons-does-not-work


        if (((ImageIcon) bt[index].getIcon()).getDescription()=="ori")
            bt[index].setIcon(tail);
         else
            bt[index].setIcon(ori);

        count++;

       System.out.printf("Action Performed %d times \n",count);
         if(count==1){  //if the card is clicked for once, the card should not flip and the index is stored in record.
           record=index;
           countS++;

       }
       String turnS=Integer.toString(countS);//parse the int and printout the turn 
     //  text3.setText(sp1+"This is turn "+turnS);
       if(count==2){
           int match1=record/4;   //Since every four cards have the same rank, I used this to get the rank very easily
           int match2=index/4;
          if(match1==match2&&record!=index){  //compare the two cards clicked
              p1++;
              score1=Integer.toString(p1);   
              text1.setText("Player 1: "+score1);  //display the score
              text3.setText(sp2+"This is turn "+turnS);
              bt[index].setEnabled(false);//remove the cards that match
              bt[record].setEnabled(false);
          }
          if(record==index){
              text3.setText(sp2+"This is turn "+turnS);//designed for the situation that the user clicks the same card
          }
          if(match1!=match2){//designed for the situation that the two cards do not match
           //time.schedule(taskFlip1,500);//delay the flip so that the user can see the cards
           //time.schedule(taskFlip2,500);


              try{   **//This part is problematic!**

                  Thread.currentThread().sleep(4000);
                  flip(index);
                  flip(record);


                }
                catch(Exception ie){

                }
              }
          text3.setText(sp2+"This is turn "+turnS);


       }

When I click on the button, the button is supposed to change the ImageIcon. It works fine without the sleep. But after I add sleep, when I click on the button, the program pauses without changing the ImageIcon! Can you tell me why? Thank you!

The action is executed by the very thread which also does handle the drawing. You block this thread. You see nothing. Game over.

This is the reason why YOU SHALL NOT BLOCK NOR DELAY THE EVENT-DISPATCHER THREAD.

Searching for the term "Event Dispatcher Thread" and "blocking" you find plenty of stuff explaining the gory details.

ActionPerformed is run in the event-dispatcher thread. Sleeping in there will stop the UI from updating.

You can use a Swing Timer instead to delay an action.

The actionPerformed() method runs in the event dispatching thread. So does the repaint system. If you sleep, you are deferring painting, and everything else. You should never sleep in this thread. If you want a deferred paint, use SwingWorker or javax.swing.Timer to start a deferred task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM