让JavaFX等到时间轴完成 [英] make JavaFX wait until Timeline finishes

查看:129
本文介绍了让JavaFX等到时间轴完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个在一个函数中运行的时间线,它们都影响一个对象,问题是当我播放第一个时间线时,另一个时间线也在下一行代码中启动,我如何才能等到第一个时间线完成然后再播放另一个时间线?

I have two timeline which runs inside one function, they are both affect one object, problem is when I play first timeline, other timeline also starts within next line of code, how can I wait until fist timeline finish then play another timeline?

推荐答案

Timeline.play()是异步的—它将在后台启动动画并立即返回.有几种方法可以实现顺序执行.哪个最合适取决于您的用例.

Timeline.play() is asynchronous—it starts the animation in the background and returns immediately. There are several ways to achieve sequential execution. Which fits best depends on your use case.

  1. 仅使用一个Timeline,并将KeyFrame从第二个时间轴移至第一个时间轴,并调整开始时间.
  2. 第一个时间表完成后,开始第二个时间表:timeline1.setOnFinished(e -> timeline2.play()).
  3. 如果可以,请使用Transition而不是Timeline并将它们包装在SequentialTransition中.
  1. Use just one Timeline and move the KeyFrames from your second timeline to the first one, with adjusted start duration.
  2. Start the second timeline when the first one is finished: timeline1.setOnFinished(e -> timeline2.play()).
  3. If you can, use Transitions instead of Timelines and wrap them in a SequentialTransition.

这篇关于让JavaFX等到时间轴完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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