在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 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 上也失败了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)