【问题标题】:Using showAndWait in the onFinished EventHandler of an Animation doesn't work在动画的 onFinished EventHandler 中使用 showAndWait 不起作用
【发布时间】:2014-03-17 18:57:31
【问题描述】:

在 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)并获得解决方法的建议。

【问题讨论】:

    标签: java animation javafx-2 eventhandler


    【解决方案1】:

    如果你包装代码以在 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 上也失败了。

    【讨论】:

      【解决方案2】:

      我已将此报告为 JavaFX (Jira issue) 中的一个错误。

      对该问题的评论:

      问题是在处理脉冲时调用 showAndWait() 时没有安排后续脉冲。

      这已在 Java 8u20 中“修复”。现在抛出异常。改动可见here

      【讨论】:

      • 我认为是这样的。如果可能,我倾向于避免使用 showAndWait();您通常可以使用 show() 并使用舞台的显示属性注册一个处理程序来实现您所需要的。
      • 嘿@bmesuere!您知道问题是否已解决!谢谢。
      • 这已在 Java 8u20 中“修复”。现在抛出异常:hg.openjdk.java.net/openjfx/8u-dev/rt/rev/c5ebcc5268b4
      猜你喜欢
      • 1970-01-01
      • 2017-11-28
      • 2015-09-05
      • 2012-04-13
      • 2013-02-18
      • 2012-08-06
      • 2015-02-19
      • 1970-01-01
      相关资源
      最近更新 更多