2017-06-04 133 views
0

這段代碼有什麼問題? 我很困惑! 我想在主舞臺上改變我的場景。在javafx中改變場景

public class SignInController { 
    @FXML 
    TextField SignInPassword; 

    @FXML 
    TextField SignInUsername; 

    @FXML 
    CheckBox RememberMe; 

    public void signUpScene(MouseEvent mouseEvent) throws IOException { 
     Timeline timeline = new Timeline(); 
     Scene SignUpScene = new Scene(FXMLLoader.load(getClass().getResource("sign up.fxml")),700,700); 
     Main.pstage.setScene(SignUpScene); 
     timeline.getKeyFrames().addAll(
       new KeyFrame(Duration.ZERO,new KeyValue(SignUpScene.getWidth(),0.0)), 
       new KeyFrame(Duration.millis(1000.0d),new KeyValue(SignUpScene.getWidth(),700.0)) 
     ); 

     timeline.play(); 
    } 
} 
+0

這將不會編譯,會嗎? –

+0

@James_D我知道但我該怎麼辦? – Mohammasd

+0

您無法創建指定兩個雙打的「KeyValue」。你沒有得到一個編譯錯誤,告訴你?你需要一個'WritableValue' - 通常是'Property'。你究竟在做什麼? –

回答

3

如果你想動畫舞臺的寬度牽着你的新場景,你可以使用一個Transition

public void signUpScene(MouseEvent mouseEvent) throws IOException { 
     Scene SignUpScene = new Scene(FXMLLoader.load(getClass().getResource("sign up.fxml")),700,700); 
     Main.pstage.setScene(SignUpScene); 

     Rectangle clip = new Rectangle(0, 700); 

     Transition animateStage = new Transition() { 
      { 
       setCycleDuration(Duration.millis(1000)); 
      } 
      @Override 
      protected void interpolate(double t) { 
       Main.pstage.setWidth(t * 700.0); 
      } 
     }; 
     animateStage.play(); 
    } 
} 

也許更好的做法是用夾子逐漸顯露出新場景:

public void signUpScene(MouseEvent mouseEvent) throws IOException { 

     Parent root = FXMLLoader.load(getClass().getResource("sign up.fxml")); 

     Scene SignUpScene = new Scene(root,700,700); 
     Main.pstage.setScene(SignUpScene); 

     Rectangle clip = new Rectangle(0, 700); 
     Timeline animate = new Timeline(
      new KeyFrame(Duration.millis(1000), 
       new KeyValue(clip.widthProperty(), 700.0)); 
     root.setClip(clip); 
     // when animation finishes, remove clip: 
     animate.setOnFinished(e -> root.setClip(null)); 
     animate.play(); 
    } 
} 
+0

非常感謝你 – Mohammasd