簡體   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