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

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

Halt JavaFX program until after Transition is complete

问题

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

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

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();

                // 等待转换完成
                translateRect1.setOnFinished(event -> {
                    // 继续执行剩余代码
                    System.out.printf("当前迭代为 %s\n", i);
                });
            }
        }
    }

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

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

英文:

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.

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(&quot;Stack Overflow MRE&quot;);
        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 &lt; 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(&quot;The current iteration is %s\n&quot;, i);
        }
    }

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

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 处理程序,例如:

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("测试输出的时机");
});

translateRect2.play();

// 此行在上述过渡仍在运行时打印
// System.out.println("测试输出的时机");

您编辑后的问题的解决方案本质上是相同的:以明显的方式进行重构,以便您想要在过渡之后运行的代码位于 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("当前迭代次数为 %s\n", i);
    }
});

translateRect2.play();
System.out.printf("当前迭代次数为 0\n");
英文:

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

    TranslateTransition translateRect2 = new TranslateTransition();
translateRect2.setNode(rect2);
translateRect2.setAutoReverse(true);
translateRect2.setCycleCount(1);
translateRect2.setToY(350);
translateRect2.setDuration(Duration.millis(5000));
translateRect2.setOnFinished(e -&gt; {
System.out.println(&quot;Testing timing of output&quot;);
});
translateRect2.play();
// This line prints while the above transitions are still running
// 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:

    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 -&gt; {
for (int i = 1; i &lt; 10; i++) {
System.out.printf(&quot;The current iteration is %s\n&quot;, i);
}
});
translateRect2.play();
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:

确定