当有线程时,swing计时器不会停止。睡在Java里面
您好,我有这个代码来显示图像与javafx
public void CantaCarta() throws InterruptedException {
startGame.setDisable(true);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
SwingUtilities.invokeLater(() -> {
for (int x=1; x<55;x++){
Image image = new Image(getClass().getResource("imgs/"+JuegoLoto.Muestra(x-1)+".jpg").toString(), true);
cantada.setImage(image);
if (x >= 54) {
System.out.print("Termina");
timer.cancel();
} else {
System.out.print(" "+x+" ");
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
}
}
}
});
}
}, 0, 1000);
}
图像将正确显示,但当图像编号54出现在屏幕中时,它将在循环中返回到1,所有这一切都是因为此
Thread.sleep(200);
我怎样才能解决这个问题?我想延迟图像之间的时间
# 1 楼答案
这一点没有意义
因此,您将获得:
因此,您将得到大约10到11次任务迭代,大部分是并行的
我建议你:
以200ms的间隔安排任务,让它在每次调用时显示下一个顺序映像,在到达最后一个映像时返回到开头或取消计时器或任何您想要的内容,并且
摆脱内部循环