0

我正在用 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() ,但我仍然无法获得宽度。请帮我解决这个问题..

4

1 回答 1

0

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

于 2013-04-15T07:43:16.323 回答