首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >混洗元素

混洗元素
EN

Stack Overflow用户
提问于 2016-03-09 05:14:07
回答 1查看 317关注 0票数 0

有没有可能在我进入堆栈后对我的元素进行混洗?因此,计划是每次都显示随机图像。我的代码如下:

代码语言:javascript
复制
/*
 * 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秒。我想展示它们,但被打乱了。

EN

回答 1

Stack Overflow用户

发布于 2016-03-09 05:22:09

Collection类有一个内置的shuffle方法。

代码语言:javascript
复制
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)))));
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35878131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档