繁体   English   中英

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

[英]How to start animation instantly after initializing the Scene in Java FX with FXML

我正在用Java FX编写带有一些精美动画的应用程序。 我想将文本节点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的内置调试器时,我发现parent的stage变量中的大多数值都是null。 我认为是因为该阶段尚未完全初始化,所以我得到了空值。 有什么办法可以检测到完整的初始化吗? 请帮我...

编辑 :即使我添加了

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM