繁体   English   中英

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

[英]How do I make Thread.sleep() delay the execution time between two methods?

我正在做一个个人项目。 理想情况下,我的程序应将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都必须始终播放到完成。 我怎样才能做到这一点?

你问,

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

最好的答案是……。
您的是一个Swing GUI,所以不,尤其是在Swing事件线程上,请不要使用Thread.sleep(...) ,因为您最终会使GUI完全进入睡眠状态,这不是您的目标。 请改用Swing计时器。

将延迟后要运行的代码放入Timer的ActionListener中,在Timer和Bingo上调用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();
         }
      });
   }
}

怎么样...

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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