我想逐个随机显示我的图像。下面的代码来显示图像逐个只有如果我把持续时间(秒),如:在幻灯片中逐个显示随机图像
new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image1)),
new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image2)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image3)),
new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), image4)),
所以,我的代码将显示图像1 firstn图像2秒,图像3三分之二等。
1)我希望它每次都显示随机图像。 2)不依赖于持续时间。因为如果我把Duration.seconds(3)
都放在它们上面,它只会显示第一个。
代码如下所示:
package imagedisplayy;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author D
*/
public class ImageDisplayy extends Application {
@Override
public void start(Stage primaryStage) {
Image image1 = new Image("file:lib/1.jpg");
Image image2 = new Image("file:lib/2.jpg");
Image image3 = new Image("file:lib/3.jpg");
Image image4 = new Image("file:lib/4.jpg");
ImageView imageView = new ImageView();
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image1)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image2)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image3)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image4)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), null))
);
timeline.play();
StackPane root = new StackPane();
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
你是什么意思“每次”? – KWeiss
@KWeiss当我运行这段代码时,我想一个接一个地随机显示图像。 – dailyadd
你为什么要让所有的时间点都一样? –