2015-05-25 34 views
0

我試圖通過「淡出」和「淡入」轉換來改變我的舞臺場景。我真的無法得到它,但根本沒有。如何使用JavaFX FadeTransition輸入輸出

第一次,它不起作用。然後,有時它可以正常工作,而其他新場景以毫秒爲單位顯示,以便繼續處理其餘的轉場。唯一完美的是FadeOut。

我想我做錯了什麼,但我不知道是什麼。 我試過用FadeTransition和Timeline,但結果總是一樣的。

我告訴你的代碼片段,其中我的工作:

// CALLED FROM控制器的時候,我想改變場景

private static FadeTransition fadeOut = new FadeTransition(); 
private static FadeTransition fadeIn = new FadeTransition(); 

public void setScene(final String resource_fxml, final String title) { 
     fadeOut.setOnFinished(new EventHandler<ActionEvent>() { 

      @Override 
      public void handle(ActionEvent event) { 
       loadScene(resource_fxml, title); 
      } 
     }); 
     fadeOut.setNode(lastRoot); 
     fadeOut.setDuration(Duration.millis(Config.TRANSITIONS_TIME)); 
     fadeOut.setFromValue(1.0); 
     fadeOut.setToValue(0.0); 
     fadeOut.play(); 
} 

private void loadScene(String resource_fxml, String title) { 
     double width = SceneManager.lastScene.getWidth(); 
     double height = SceneManager.lastScene.getHeight(); 

     Parent newRoot = null; 
     try { 
      newRoot = FXMLLoader.load(getClass().getResource(resource_fxml)); 
     } catch (IOException ex) { 
      log.error("Resource not found"); 
     } 

//  DoubleProperty opacity = newRoot.opacityProperty(); 
//  Timeline fadeIn = new Timeline(
//    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)), 
//    new KeyFrame(Duration.millis(Config.TRANSITIONS_TIME), new KeyValue(opacity, 1.0)) 
//  ); 
//  fadeIn.play(); 

     fadeOut.setNode(newRoot); 
     fadeOut.setDuration(Duration.millis(Config.TRANSITIONS_TIME)); 
     fadeOut.setFromValue(0.0); 
     fadeOut.setToValue(1.0); 
     fadeOut.play(); 

     lastScene = new Scene(newRoot, width, height); 
     mainStage.setTitle(title); 
     mainStage.setScene(lastScene); 
     mainStage.show(); 

     lastRoot = newRoot; 

    } 

編輯:我展示了最終代碼(」 loadScene「),它工作得很好。謝謝你的提示,Steven Van Impe!

private void loadScene(String resource_fxml, String title) { 
     double width = SceneManager.lastScene.getWidth(); 
     double height = SceneManager.lastScene.getHeight(); 

     Parent newRoot = null; 
     try { 
      newRoot = XMLLoader.load(getClass().getResource(resource_fxml)); 
     } catch (IOException ex) { 
      log.error("Resource not found"); 
     } 

     fadeIn.setNode(newRoot); 
     fadeIn.setDuration(Duration.millis(Config.TRANSITIONS_TIME)); 
     fadeIn.setFromValue(0.0); 
     fadeIn.setToValue(1.0); 

     DoubleProperty opacity = newRoot.opacityProperty(); 
     opacity.set(0); 
     lastScene = new Scene(newRoot, width, height); 
     mainStage.setTitle(title); 
     mainStage.setScene(lastScene); 

     fadeIn.play(); 

     lastRoot = newRoot; 
    } 

回答

0

您在loadScene中再次使用fadeOut。我拿它你想在那裏使用fadeIn?另外,當newRoot甚至不是場景圖的一部分時,您就開始動畫。您可能需要先嚐試設置場景(但以不透明度0開始newRoot關閉),然後開始動畫。

作爲一般說明,我還建議您在切換屏幕時不要交換場景(甚至是場景的根目錄),而只需在GUI中更換窗格。這會給你更多的動畫靈活性。例如,您可以淡入新窗格並同時淡出舊窗格,但如果使用場景則不可能。

+0

您對換窗格很有意思。但我有一個問題。如果我使用窗格,所有這些窗格應該在相同的.fxml,這導致在同一個控制器中的所有窗格?如果答案是肯定的,我認爲這將很難合作。 –

+0

這沒有必要。每個屏幕可以有一個單獨的FXML和控制器。您可以根據需要簡單地加載它們。我也推薦使用'fx:root'構造。它使得封裝好。有關'fx:root'的更多信息,請參見[我的答案](http://stackoverflow.com/questions/30446878/how-to-pass-arguments-in-javafx/30457992#30457992)。 –