gpt4 book ai didi

java - 在动画的 onFinished EventHandler 中使用 showAndWait 不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 20:02:34 25 4
gpt4 key购买 nike

在 JavaFx 中,我想在动画结束后显示模式对话框。出于某种原因,在动画结束后执行的 EventHandler 中调用 showAndWait 不起作用。显示了一个新窗口,但其中似乎没有绘制任何内容。

这个例子演示了这个问题:

public void start(Stage primaryStage) {
Rectangle rect = RectangleBuilder.create().width(200).height(200).fill(Color.RED).build();

StackPane root = new StackPane();
root.getChildren().add(rect);

Timeline animation = new Timeline();
animation.getKeyFrames().addAll(
new KeyFrame(new Duration(1000),
new KeyValue(rect.widthProperty(), 100),
new KeyValue(rect.heightProperty(), 100)),
new KeyFrame(new Duration(2000),
new KeyValue(rect.widthProperty(), 300),
new KeyValue(rect.heightProperty(), 300))
);
animation.setOnFinished(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent t) {
Stage stage = new Stage();
StackPane pane = new StackPane();
pane.getChildren().add(new Label("Hello world"));
stage.setScene(new Scene(pane, 100, 100));
stage.showAndWait();
}
});
animation.setCycleCount(1);

Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();

animation.play();
}

完整代码可以在 https://gist.github.com/bmesuere/9605866 找到

我想知道为什么这不起作用(在我的 macbook 上使用 java 1.7.0_51)并获得解决方法的建议。

最佳答案

如果您包装代码以在 Platform.runLater() 中显示舞台,它似乎可以工作:

animation.setOnFinished(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent t) {
Platform.runLater(new Runnable() {
@Override
public void run() {
Stage stage = new Stage();
StackPane pane = new StackPane();
pane.getChildren().add(new Label("Hello world"));
stage.setScene(new Scene(pane, 100, 100));
stage.showAndWait();
}
});

}
});

不知道为什么。它在 Java FX 8 上也失败了。

关于java - 在动画的 onFinished EventHandler 中使用 showAndWait 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22463004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com