【发布时间】: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 帖子。
问题是,
启动应用程序后,点击Pane 1
Button。 -> 转换将首先显示pane2,然后是pane1。现在点击Pane 3
Button-> 转换将首先显示pane2,然后是pane3。现在点击Pane 2
Button-> 转换会显示pane2并且上面2点提到的问题不再出现。
为什么过渡显示 pane2 在点 1 和 2 中显示实际窗格之前?是因为不透明度设置吗?
为什么在第 3 点之后问题得到解决?
如何在不显示第三个窗格的情况下使转换工作到 FadeIn 和 FadeOut 各自的 Pane?
【问题讨论】:
-
我没有这样做,但我猜你可以跟踪前端节点。单击按钮时,在该节点上运行淡出转换。使用转换的 onfinished 在新的前端节点上开始淡入。确保用淡入的节点替换旧的前端。