暂停 JavaFX 程序直到转换完成

Halt JavaFX program until after Transition is complete

我正在制作一个使用多个 TranslateTransitions to move multiple Nodes across a Pane 的 JavaFX 程序。我希望程序在转换完成之前停止执行。

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Stack Overflow MRE");
        Pane root = new Pane();

        Rectangle rect1 = new Rectangle(25, 100, Color.AQUA);
        rect1.setX(10);
        rect1.setY(10);

        Rectangle rect2 = new Rectangle(25, 100, Color.BISQUE);
        rect2.setX(465);
        rect2.setY(10);

        root.getChildren().addAll(rect1, rect2);
        primaryStage.setScene(new Scene(root, 500, 500));
        primaryStage.show();
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                TranslateTransition translateRect1 = new TranslateTransition();
                translateRect1.setNode(rect1);
                translateRect1.setAutoReverse(true);
                translateRect1.setCycleCount(1);
                translateRect1.setToY(350);
                translateRect1.setDuration(Duration.millis(5000));
                translateRect1.play();

                TranslateTransition translateRect2 = new TranslateTransition();
                translateRect2.setNode(rect2);
                translateRect2.setAutoReverse(true);
                translateRect2.setCycleCount(1);
                translateRect2.setToY(350);
                translateRect2.setDuration(Duration.millis(5000));
                translateRect2.play();
            }
            // The loop will continue iterating while the above transitions are still running
            // I need the loop to pause until they complete
            System.out.printf("The current iteration is %s\n", i);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在上面的代码示例中,两个转换同时发生,这是我想要的,但是在转换完成之前循环继续。我希望循环暂停,直到转换完成,然后继续进行下一次迭代。我如何让程序先完成转换,然后再继续编写其余代码?

只需将 onFinished 处理程序用于您希望代码在其后执行的转换,例如

    TranslateTransition translateRect2 = new TranslateTransition();
    translateRect2.setNode(rect2);
    translateRect2.setAutoReverse(true);
    translateRect2.setCycleCount(1);
    translateRect2.setToY(350);
    translateRect2.setDuration(Duration.millis(5000));

    translateRect2.setOnFinished(e -> {
        System.out.println("Testing timing of output");
    });

    translateRect2.play();

    // This line prints while the above transitions are still running
    // System.out.println("Testing timing of output");

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

    TranslateTransition translateRect1 = new TranslateTransition();
    translateRect1.setNode(rect1);
    translateRect1.setAutoReverse(true);
    translateRect1.setCycleCount(1);
    translateRect1.setToY(350);
    translateRect1.setDuration(Duration.millis(5000));
    translateRect1.play();

    TranslateTransition translateRect2 = new TranslateTransition();
    translateRect2.setNode(rect2);
    translateRect2.setAutoReverse(true);
    translateRect2.setCycleCount(1);
    translateRect2.setToY(350);
    translateRect2.setDuration(Duration.millis(5000));

    translateRect2.setOnFinished(e -> {
        for (int i = 1; i < 10; i++) {
            System.out.printf("The current iteration is %s\n", i);
        }
    });

    translateRect2.play();
    System.out.printf("The current iteration is 0\n");