如何使用Thread.sleep,Platform.runLater和javafx

时间:2019-05-26 00:41:05

标签: java javafx

下面的代码应该调用“ change”方法,并在每次延迟之后使用javafx显示该方法调用的输出。在这种情况下,它应该等待20秒钟来更新屏幕,然后等待3秒钟,然后由于再次调用“ change”方法而对具有不同内容的屏幕显示另一个更新。相反,它所做的只是在javafx窗口上显示原始内容,然后等待23秒,并在第二次调用change方法之后显示应该是什么屏幕,并且似乎跳过了对该方法的第一次调用,但是仍然等待第一个方法调用的延迟。如果有人可以告诉我如何实现在javafx屏幕上指定的时间后执行两个方法调用的目标,我将不胜感激。请告诉我如何修复代码。

        //displays stuff on the screen
        group.getChildren ().add (imgView);//adds the background image to the screen
        group.getChildren ().add (s1View);//adds the first step of the person kicking the ball


        primaryStage.show ();//displays the stage

        Platform.runLater (() -> change (group, s1View, s2View, 20000));
        Platform.runLater (() -> change (group, s2View, s3View, 3000));
      }



      public void change (Group group, ImageView rView, ImageView nView, int milliseconds)
      {
        try
        {
          Thread.sleep (milliseconds);
        }
       catch (Exception e)
        {
        }
        group.getChildren ().remove (rView);
        group.getChildren ().add (nView);
      }

0 个答案:

没有答案