暂停 JavaFX 程序,直到过渡效果完成。

huangapple go评论102阅读模式
英文:

Halt JavaFX program until after Transition is complete

问题

以下是您要求的翻译内容:

我正在制作一个使用多个 TranslateTransition 的 JavaFX 程序,以将多个 NodePane 中移动。我希望程序在转换完成之后暂停执行。

  1. import javafx.animation.TranslateTransition;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.layout.Pane;
  5. import javafx.scene.paint.Color;
  6. import javafx.scene.shape.Rectangle;
  7. import javafx.stage.Stage;
  8. import javafx.util.Duration;
  9. public class Main extends Application {
  10. @Override
  11. public void start(Stage primaryStage) {
  12. primaryStage.setTitle("Stack Overflow MRE");
  13. Pane root = new Pane();
  14. Rectangle rect1 = new Rectangle(25, 100, Color.AQUA);
  15. rect1.setX(10);
  16. rect1.setY(10);
  17. Rectangle rect2 = new Rectangle(25, 100, Color.BISQUE);
  18. rect2.setX(465);
  19. rect2.setY(10);
  20. root.getChildren().addAll(rect1, rect2);
  21. primaryStage.setScene(new Scene(root, 500, 500));
  22. primaryStage.show();
  23. for (int i = 0; i < 10; i++) {
  24. if (i == 0) {
  25. TranslateTransition translateRect1 = new TranslateTransition();
  26. translateRect1.setNode(rect1);
  27. translateRect1.setAutoReverse(true);
  28. translateRect1.setCycleCount(1);
  29. translateRect1.setToY(350);
  30. translateRect1.setDuration(Duration.millis(5000));
  31. translateRect1.play();
  32. TranslateTransition translateRect2 = new TranslateTransition();
  33. translateRect2.setNode(rect2);
  34. translateRect2.setAutoReverse(true);
  35. translateRect2.setCycleCount(1);
  36. translateRect2.setToY(350);
  37. translateRect2.setDuration(Duration.millis(5000));
  38. translateRect2.play();
  39. // 等待转换完成
  40. translateRect1.setOnFinished(event -> {
  41. // 继续执行剩余代码
  42. System.out.printf("当前迭代为 %s\n", i);
  43. });
  44. }
  45. }
  46. }
  47. public static void main(String[] args) {
  48. launch(args);
  49. }
  50. }

在上述代码示例中,两个转换同时发生,这是我想要的,然而循环在转换完成之前就继续执行了。我希望循环在转换完成之前暂停,然后再继续执行下一次迭代。我应该如何让程序在继续执行剩余代码之前先完成转换呢?

英文:

I am making a JavaFX program that uses multiple TranslateTransitions to move multiple Nodes across a Pane. I want the program to halt execution until after the transitions are complete.

  1. import javafx.animation.TranslateTransition;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.layout.Pane;
  5. import javafx.scene.paint.Color;
  6. import javafx.scene.shape.Rectangle;
  7. import javafx.stage.Stage;
  8. import javafx.util.Duration;
  9. public class Main extends Application {
  10. @Override
  11. public void start(Stage primaryStage) {
  12. primaryStage.setTitle(&quot;Stack Overflow MRE&quot;);
  13. Pane root = new Pane();
  14. Rectangle rect1 = new Rectangle(25, 100, Color.AQUA);
  15. rect1.setX(10);
  16. rect1.setY(10);
  17. Rectangle rect2 = new Rectangle(25, 100, Color.BISQUE);
  18. rect2.setX(465);
  19. rect2.setY(10);
  20. root.getChildren().addAll(rect1, rect2);
  21. primaryStage.setScene(new Scene(root, 500, 500));
  22. primaryStage.show();
  23. for (int i = 0; i &lt; 10; i++) {
  24. if (i == 0) {
  25. TranslateTransition translateRect1 = new TranslateTransition();
  26. translateRect1.setNode(rect1);
  27. translateRect1.setAutoReverse(true);
  28. translateRect1.setCycleCount(1);
  29. translateRect1.setToY(350);
  30. translateRect1.setDuration(Duration.millis(5000));
  31. translateRect1.play();
  32. TranslateTransition translateRect2 = new TranslateTransition();
  33. translateRect2.setNode(rect2);
  34. translateRect2.setAutoReverse(true);
  35. translateRect2.setCycleCount(1);
  36. translateRect2.setToY(350);
  37. translateRect2.setDuration(Duration.millis(5000));
  38. translateRect2.play();
  39. }
  40. // The loop will continue iterating while the above transitions are still running
  41. // I need the loop to pause until they complete
  42. System.out.printf(&quot;The current iteration is %s\n&quot;, i);
  43. }
  44. }
  45. public static void main(String[] args) {
  46. launch(args);
  47. }
  48. }

In the above code sample, the two transitions occur simultaneously, which is what I want, however the loop continues before the transitions complete. I want the loop to pause until the transitions are complete, and then move on to the next iterations. How would I get the program to first complete the transitions before moving on to the rest of the code?

答案1

得分: 2

只需为您想要执行代码的过渡添加一个 onFinished 处理程序,例如:

  1. TranslateTransition translateRect2 = new TranslateTransition();
  2. translateRect2.setNode(rect2);
  3. translateRect2.setAutoReverse(true);
  4. translateRect2.setCycleCount(1);
  5. translateRect2.setToY(350);
  6. translateRect2.setDuration(Duration.millis(5000));
  7. translateRect2.setOnFinished(e -> {
  8. System.out.println("测试输出的时机");
  9. });
  10. translateRect2.play();
  11. // 此行在上述过渡仍在运行时打印
  12. // System.out.println("测试输出的时机");

您编辑后的问题的解决方案本质上是相同的:以明显的方式进行重构,以便您想要在过渡之后运行的代码位于 onFinished 处理程序中:

  1. TranslateTransition translateRect1 = new TranslateTransition();
  2. translateRect1.setNode(rect1);
  3. translateRect1.setAutoReverse(true);
  4. translateRect1.setCycleCount(1);
  5. translateRect1.setToY(350);
  6. translateRect1.setDuration(Duration.millis(5000));
  7. translateRect1.play();
  8. TranslateTransition translateRect2 = new TranslateTransition();
  9. translateRect2.setNode(rect2);
  10. translateRect2.setAutoReverse(true);
  11. translateRect2.setCycleCount(1);
  12. translateRect2.setToY(350);
  13. translateRect2.setDuration(Duration.millis(5000));
  14. translateRect2.setOnFinished(e -> {
  15. for (int i = 1; i < 10; i++) {
  16. System.out.printf("当前迭代次数为 %s\n", i);
  17. }
  18. });
  19. translateRect2.play();
  20. System.out.printf("当前迭代次数为 0\n");
英文:

Just use an onFinished handler for the transition after which you want the code to execute, e.g.

  1. TranslateTransition translateRect2 = new TranslateTransition();
  2. translateRect2.setNode(rect2);
  3. translateRect2.setAutoReverse(true);
  4. translateRect2.setCycleCount(1);
  5. translateRect2.setToY(350);
  6. translateRect2.setDuration(Duration.millis(5000));
  7. translateRect2.setOnFinished(e -&gt; {
  8. System.out.println(&quot;Testing timing of output&quot;);
  9. });
  10. translateRect2.play();
  11. // This line prints while the above transitions are still running
  12. // System.out.println(&quot;Testing timing of output&quot;);

The solution for your edited question is essentially the same: refactor it in the obvious way so that the code that you want to run after the transitions is in the onFinished handler:

  1. TranslateTransition translateRect1 = new TranslateTransition();
  2. translateRect1.setNode(rect1);
  3. translateRect1.setAutoReverse(true);
  4. translateRect1.setCycleCount(1);
  5. translateRect1.setToY(350);
  6. translateRect1.setDuration(Duration.millis(5000));
  7. translateRect1.play();
  8. TranslateTransition translateRect2 = new TranslateTransition();
  9. translateRect2.setNode(rect2);
  10. translateRect2.setAutoReverse(true);
  11. translateRect2.setCycleCount(1);
  12. translateRect2.setToY(350);
  13. translateRect2.setDuration(Duration.millis(5000));
  14. translateRect2.setOnFinished(e -&gt; {
  15. for (int i = 1; i &lt; 10; i++) {
  16. System.out.printf(&quot;The current iteration is %s\n&quot;, i);
  17. }
  18. });
  19. translateRect2.play();
  20. System.out.printf(&quot;The current iteration is 0\n&quot;);

huangapple
  • 本文由 发表于 2020年10月5日 23:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64211325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定