进入堆叠后是否可以随机播放元素? 所以,计划是每次都显示随机图像。 我的代码如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package imagedisplay;
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 ImageDisplay extends Application {
@Override
public void start(Stage primaryStage) {
Image Img1 = new Image("file:lib/1.jpg");
Image Img2 = new Image("file:lib/2.jpg");
Image Img3 = new Image("file:lib/3.jpg");
Image Img4 = new Image("file:lib/4.jpg");
ImageView ViewImg = new ImageView();
Timeline tl = new Timeline(
new KeyFrame(Duration.seconds(25), new KeyValue(ViewImg.imageProperty(), Img1)),
new KeyFrame(Duration.seconds(20), new KeyValue(ViewImg.imageProperty(), Img2)),
new KeyFrame(Duration.seconds(15), new KeyValue(ViewImg.imageProperty(), Img3)),
new KeyFrame(Duration.seconds(10), new KeyValue(ViewImg.imageProperty(), Img4)),
new KeyFrame(Duration.seconds(5), new KeyValue(ViewImg.imageProperty(), null)));
tl.play();
StackPane st = new StackPane();
st.getChildren().add(ViewImg);
primaryStage.setScene(new Scene(st, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
基本上这个代码的作用是,它逐个显示一些图像,每个图像持续5秒。我想展示他们,但是他们已经甩掉了。
答案 0 :(得分:-1)
Collections类有一个内置的shuffle方法。
Timeline tl = new Timeline(
Collections.shuffle(Arrays.asList(
new KeyFrame(Duration.seconds(25), new KeyValue(ViewImg.imageProperty(), Img1)),
new KeyFrame(Duration.seconds(20), new KeyValue(ViewImg.imageProperty(), Img2)),
new KeyFrame(Duration.seconds(15), new KeyValue(ViewImg.imageProperty(), Img3)),
new KeyFrame(Duration.seconds(10), new KeyValue(ViewImg.imageProperty(), Img4)),
new KeyFrame(Duration.seconds(5), new KeyValue(ViewImg.imageProperty(), null)))));