在javafx中为图像添加计时器 [英] Add timer for images in javafx

查看:1151
本文介绍了在javafx中为图像添加计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javafx中为我的图像添加计时器,例如,首先显示我的第一张图像约3秒,然后显示我的第二张图像约5秒钟,之后没有显示任何内容。对此有什么看法?

i want to add timer for my images in javafx for example first ,for about 3 seconds my first image displayed then for about 5 second my second image displayed and after that nothing shown.any idea about that ?

推荐答案

使用 时间表 更新 imageProperty http://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html\"rel =nofollow> ImageView

Use a Timeline to update the imageProperty of an ImageView:

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;

public class ImageDisplayTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Image image1 = new Image("...") ;
        Image image2 = new Image("...")  ;
        ImageView imageView = new ImageView();
        Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)),
                new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image2)),
                new KeyFrame(Duration.seconds(8), 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);
    }
}

这篇关于在javafx中为图像添加计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆