如何使Thread.sleep()延迟两个方法之间的执行时间?

时间:2015-02-14 23:20:57

标签: java delay

我正在从事个人项目。理想情况下,我的程序应该将jlabels图标设置为某个gif,等待gif结束,然后将其设置为空白png。我的程序应该在每次单击另一个标签时执行此操作(我使用事件监听器将标签用作按钮,按钮很难看)。不幸的是,按下“按钮”时,gif会立即被空白的png替换,因为没有延迟或等待。

public void battle() {

ImageIcon active = new  ImageIcon(getClass().getClassLoader().getResource("resource/attacks/Animation-attackbasic_DEFAULT.gif"));
ImageIcon blank = new ImageIcon(getClass().getClassLoader().getResource("resource/attacks/Animation-blank_DEFAULT.png"));

playerAttackDisplay.setIcon(active); //This sets the Jlabel icon to the gif

try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(aqGUI.class.getName()).log(Level.SEVERE, null, ex);}

playerAttackDisplay.setIcon(blank);
}

我想在一个带有int seconds参数的方法中放入一些代码,然后只要我需要延迟就调用它。最简单的方法是什么?

修改

所以我使用了Hovercraft Full of Eels这样的方法

public void delay(int time) {
final ImageIcon blank = new ImageIcon(getClass().getClassLoader().getResource("resource/attacks/Animation-blank_DEFAULT.png"));
new Timer(time, new ActionListener() {

public void actionPerformed(ActionEvent arg0) 
     {playerAttackDisplay.setIcon(blank);}
      }).start();}

public void battle() {
final ImageIcon I = new ImageIcon(getClass().getClassLoader().getResource("resource/attacks/Animation-attackbasic_DEFAULT.gif"));

    playerAttackDisplay.setIcon(I);
    delay(500);
}

每次用户按下GUI中的按钮时,都会执行“battle()”方法。在按下它大约两次之后,在设置的gif和设置的png之间存在越来越小的延迟。这导致gif动画在完成之前被切断。经过大约四到五次按压后,gif似乎立即被切断了。

我已经玩过各种延迟时间,但总是在玩GIF完成时没有一致。有没有办法得到一致的延迟?为了我的目的,gif必须始终按每按一次按钮完成播放。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

你问,

  

如何使Thread.sleep()延迟两个方法之间的执行时间?

最好的答案是,......你没有 你的是一个Swing GUI,所以不,不要特别在Swing事件线程上使用Thread.sleep(...)因为你最终会把你的GUI完全置于睡眠状态,这不是你的目标。改为使用Swing Timer。

在延迟进入Timer的ActionListener之后运行代码,在Timer和宾果游戏上调用start()

例如,

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class TimerImageSwapper {
   public static final String[] IMAGE_URLS = {
      "http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/"
      + "600px_Amaranto_con_cavallo_rampante.png/120px-600px_Amaranto_con_cavallo_rampante.png",
      "http://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/"
      + "600px_Blu_con_Croce_del_Sud.png/120px-600px_Blu_con_Croce_del_Sud.png",
      "http://upload.wikimedia.org/wikipedia/commons/thumb/1/10/"
      + "600px-Flag_of_Portugal_and_Angola_v1.png/120px-600px-Flag_of_Portugal_and_Angola_v1.png",
      "http://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/"
      + "Flag_of_Meskhetian_Turks_1.svg/120px-Flag_of_Meskhetian_Turks_1.svg.png",
      "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/"
      + "Greece_flag_300.png/120px-Greece_flag_300.png",
      "http://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/"
      + "National_Flag_Kingdom_of_Italy.png/120px-National_Flag_Kingdom_of_Italy.png",
      "http://upload.wikimedia.org/wikipedia/commons/thumb/9/98/"
      + "Flag_of_the_Kumukh_people.png/120px-Flag_of_the_Kumukh_people.png" };


   private ImageIcon[] icons = new ImageIcon[IMAGE_URLS.length];
   private JLabel mainLabel = new JLabel();

   private int iconIndex = 0;;

   public TimerImageSwapper(int timerDelay) throws IOException {
      for (int i = 0; i < icons.length; i++) {
         URL imgUrl = new URL(IMAGE_URLS[i]);
         BufferedImage image = ImageIO.read(imgUrl);
         icons[i] = new ImageIcon(image);
      }
      int gap = 25;
      mainLabel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));

      mainLabel.setIcon(icons[iconIndex]);

      new Timer(timerDelay, new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            iconIndex++;
            iconIndex %= IMAGE_URLS.length;
            mainLabel.setIcon(icons[iconIndex]);
         }
      }).start();
   }

   public Component getMainComponent() {
      return mainLabel;
   }

   private static void createAndShowGui() {
      TimerImageSwapper timerImageSwapper;
      try {
         timerImageSwapper = new TimerImageSwapper(1000);
         JFrame frame = new JFrame("Timer Image Swapper");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(timerImageSwapper.getMainComponent());
         frame.pack();
         frame.setLocationByPlatform(true);
         frame.setVisible(true);

      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:0)

怎么样......

public void battle() {

ImageIcon active = new  ImageIcon(getClass().getClassLoader().getResource("resource/attacks/Animation-attackbasic_DEFAULT.gif"));
ImageIcon blank = new ImageIcon(getClass().getClassLoader().getResource("resource/attacks/Animation-blank_DEFAULT.png"));

playerAttackDisplay.setIcon(active,Long timeToWait); //This sets the Jlabel icon to the gif

try {
Thread.sleep(timeToWait);
} catch (InterruptedException ex) {
Logger.getLogger(aqGUI.class.getName()).log(Level.SEVERE, null, ex);}

playerAttackDisplay.setIcon(blank);
}