【问题标题】:JavaFX : StackPane Sequential TransitionJavaFX:StackPane 顺序转换
【发布时间】:2019-08-13 05:55:08
【问题描述】:

我正在尝试在 3 个不同的 AnchorPane(点击 Button)和 FadeTransition 之间切换,下面是我的代码,

public class TestSlide extends Application {
    private ObjectBinding<Node> frontNode;

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();
        AnchorPane pane1 = new AnchorPane(new Button("1"));
        AnchorPane pane2 = new AnchorPane(new Button("2"));
        AnchorPane pane3 = new AnchorPane(new Button("3"));
        root.getChildren().addAll(pane1, pane2, pane3);

        handleAnimation(root);

        BorderPane border= new BorderPane(root);

        HBox bottom = new HBox(10);
        Button front1 = new Button("Pane 1");
        Button front2 = new Button("Pane 2");
        Button front3 = new Button("Pane 3");
        front1.setOnAction((ActionEvent event) -> {
            pane1.toFront();
        });
        front2.setOnAction((ActionEvent event) -> {
            pane2.toFront();
        });
        front3.setOnAction((ActionEvent event) -> {
            pane3.toFront();
        });
        bottom.getChildren().addAll(front1, front2, front3);
        border.setBottom(bottom);

        Scene scene = new Scene(border,400,400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private void handleAnimation(StackPane root) {
        frontNode = Bindings.valueAt(root.getChildren(),
                Bindings.size(root.getChildren()).subtract(1));
        frontNode.addListener((obs, oldNode, newNode) -> {
            SequentialTransition fadeOutIn = new SequentialTransition();
            if (oldNode != null) {
                FadeTransition fadeOut = new FadeTransition(Duration.millis(500), oldNode);
                fadeOut.setToValue(0);
                fadeOutIn.getChildren().add(fadeOut);
            }
            if (newNode != null) {
                FadeTransition fadeIn = new FadeTransition(Duration.millis(500), newNode);
                fadeIn.setFromValue(0);
                fadeIn.setToValue(1);
                fadeOutIn.getChildren().add(fadeIn);
            }
            fadeOutIn.play();
        });
    }

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

handleAnimation 方法引用自另一个 SO 帖子。

问题是,

  1. 启动应用程序后,点击Pane 1 Button。 -> 转换将首先显示pane2,然后是pane1

  2. 现在点击Pane 3 Button -> 转换将首先显示pane2,然后是pane3

  3. 现在点击Pane 2 Button -> 转换会显示pane2 并且上面2点提到的问题不再出现。

为什么过渡显示 pane2 在点 1 和 2 中显示实际窗格之前?是因为不透明度设置吗?

为什么在第 3 点之后问题得到解决?

如何在不显示第三个窗格的情况下使转换工作到 FadeInFadeOut 各自的 Pane

【问题讨论】:

  • 我没有这样做,但我猜你可以跟踪前端节点。单击按钮时,在该节点上运行淡出转换。使用转换的 onfinished 在新的前端节点上开始淡入。确保用淡入的节点替换旧的前端。

标签: java javafx


【解决方案1】:

这里的问题是StackPane 的子节点的初始状态是错误的:所有节点的不透明度为 1。当没有动画运行时,您想要的状态是所有节点,但最后一个完全透明(不透明度 = 0 ) 和最后一个完全不透明(不透明度 = 1)。您应该能够通过正确初始化不透明度来解决此问题:

root.getChildren().addAll(pane1, pane2, pane3);

// set opacity for all but the last child to 0
List<Node> children = root.getChildren();
for (int i = children.size()-2; i >= 0; i--) {
    children.get(i).setOpacity(0);
}

否则会发生以下情况:

就在pane1.toFront() 之后。请注意 (SequentialTransition 确保动画开始的状态已建立。

最顶层的节点是列表中的最后一个子节点,----... 位于可见“层”旁边。

Pane 1: opacity = 0
Pane 3: opacity = 1 ------------------------------
Pane 2: opacity = 1

现在SequentialTransition 的前半部分完成后,如下所示:

Pane 1: opacity = 0
Pane 3: opacity = 0
Pane 2: opacity = 1 ------------------------------

动画完成后:

Pane 1: opacity = 1 ------------------------------
Pane 3: opacity = 0
Pane 2: opacity = 1

使用pane3.toFront() 会产生类似的结果:

Pane 3: opacity = 0
Pane 1: opacity = 1 ------------------------------
Pane 2: opacity = 1
Pane 3: opacity = 0
Pane 1: opacity = 0
Pane 2: opacity = 1 ------------------------------
Pane 3: opacity = 1 ------------------------------
Pane 1: opacity = 0
Pane 2: opacity = 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2015-08-12
    • 2021-10-23
    相关资源
    最近更新 更多