使用 JavaFX 从图像的 ArrayList 中进行幻灯片放映 [英] Slideshow from an ArrayList of images with JavaFX
问题描述
我正在尝试通过获取图像的 ArrayList
并使用 for
循环将它们放入 ImageView
来创建幻灯片.但是,为了暂停"程序以便每个图像都可见,我尝试使用 Thread.sleep
.不幸的是,这并没有达到预期的效果.为了解决这个问题,我无休止的谷歌搜索使我怀疑我应该使用 Timeline
类,但我不知道如何实现它.任何帮助将非常感激.我功能失调的代码看起来像这样:
I'm trying to create a slideshow by taking an ArrayList
of images and putting them into an ImageView
using a for
loop. However, in order to "pause" the program so that each image will be visible, I am trying to use Thread.sleep
. This unfortunately hasn't had the desired effect. In trying to remedy this, my endless googling leads me to suspect that I should be using the Timeline
class, but I have no idea how to implement it. Any help would be much appreciated. My dysfunctional code as it stands looks like this:
for (int i = 0; i < imageArrayList.size(); i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
slideshowImageView.setImage(imageArrayList.get(i));
}
推荐答案
在谷歌上搜索了很长时间后想到了
Figured it after a good long while of googling
int count; //declared as global variable
//then the working logic in my eventhandler
Task task = new Task<Void>() {
@Override
public Void call() throws Exception {
for (int i = 0; i < imageArrayList.size(); i++) {
Platform.runLater(new Runnable() {
@Override
public void run() {
slideshowImageView.setImage(imageArrayList.get(slideshowCount));
slideshowCount++;
if (slideshowCount >= imageArrayList.size()) {
slideshowCount = 0;
}
}
});
Thread.sleep(1000);
}
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
});
这篇关于使用 JavaFX 从图像的 ArrayList 中进行幻灯片放映的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!