我正在开展一个个人项目。理想情况下,我的程序应该将 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 秒参数的方法中,然后在需要延迟时调用它。最简单的方法是什么?
编辑
所以我像这样使用了 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 都必须始终播放完整。我怎样才能做到这一点?
最佳答案
你问,
How do I make Thread.sleep() delay the execution time between two methods?
最好的答案是,...你不知道。
你的是 Swing GUI,所以不,不要使用 Thread.sleep(...)
特别是在 Swing 事件线程上,因为您最终将使 GUI 完全进入休眠状态,这不是您的目标。请改用 Swing 计时器。
将延迟后运行的代码放入计时器的 ActionListener 中,调用 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();
}
});
}
}
关于java - 如何使 Thread.sleep() 延迟两个方法之间的执行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28521385/