Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用FXML初始化Java FX中的场景后立即启动动画_Java_Animation_Javafx 2_Fxml - Fatal编程技术网

如何在使用FXML初始化Java FX中的场景后立即启动动画

如何在使用FXML初始化Java FX中的场景后立即启动动画,java,animation,javafx-2,fxml,Java,Animation,Javafx 2,Fxml,我正在用JavaFX编写一个应用程序,其中包含一些奇特的动画。 我想将文本节点text_1从左侧滑动到可用舞台宽度的20% public class SplashController implements Initializable { public Democracy parent = null; @FXML public Text text1; public Text text2; public Text text3; public void animateIntro() {

我正在用JavaFX编写一个应用程序,其中包含一些奇特的动画。 我想将文本节点text_1从左侧滑动到可用舞台宽度的20%

public class SplashController implements Initializable {
public Democracy parent = null;

@FXML
public Text text1;
public Text text2;
public Text text3;

   public void animateIntro() {
       TranslateTransition t1_transl_1 = new TranslateTransition(new Duration(1500d),text1);
       t1_transl_1.setFromX(-100d);
       t1_transl_1.setByX((double)((20/100) * parent.getStage().getWidth()+100));
       // Here Democracy class has a static method to return its instance, which is assigned on class creation. I want to slide in the text_1 node to 20% of the total window width.
       //Here I am always getting **NaN**
       System.out.println((20d/100d) * parent.getStage().getWidth()); // Getting NaN
       System.out.println(parent.getStage().getScene().getWidth()); // Throwing Exception
       FadeTransition t1_fade_in = new FadeTransition(new Duration(1000d), text1);
       t1_fade_in.setFromValue(0);
       t1_fade_in.setToValue(100d);

       ParallelTransition t1_ptrans_1 = new ParallelTransition(text1,t1_fade_in,t1_transl_1);
       //With parallel transition I want to fade in and slide in the node at the same time.
       t1_ptrans_1.play();

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        parent = Democracy.getInstance();
        animateIntro();
    }    


}
当我使用Netbeans的内置调试器时,我发现父级的stage变量中的大多数值都是null。我想这是因为这个阶段没有完全初始化,我得到的是空值。是否有任何解决方法来检测完整的初始化?请帮帮我

编辑:即使在我添加了

while(!parent.getStage().isShowing()) {
try{
    sleep(100);
} catch(Exception e){}

循环以确保仅在舞台可见后调用animateIntro(),我仍然无法获取宽度。请帮我解决这个问题。

您可以尝试将侦听器绑定到stage并等待实际包含数字的更改。

Democracy类的职责是什么?更重要的是,您如何称呼它?您在哪里设置并调用FXMLLoader?