在JavaFX FXML应用程序中实现FadeTransition

时间:2015-04-01 19:26:21

标签: java javafx transition fxml

我已经看到在JavaFX应用程序中实现了淡入淡出,但是如何在FXML应用程序中实现它呢?

这就是oracle docs应该如何做到的:

FadeTransition ft = new FadeTransition(Duration.millis(3000), someNode);
ft.setFromValue(0.1);
ft.setToValue(1.0);
ft.play();

这是加载应用程序的主要内容:

public class MainApp extends Application {

private final double MINIMUM_WINDOW_WIDTH = 500.0;
private final double MINIMUM_WINDOW_HEIGHT = 400.0;
private final double MAXIMUM_WINDOW_WIDTH = 800.0;
private final double MAXIMUM_WINDOW_HEIGHT = 600.0;

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("MainApp.fxml"));

    Scene scene = new Scene(root);

    //Set window title
    stage.setTitle("My app");

    //Set minimum window size
    stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
    stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);

    //Set maximum window size
    stage.setMaxWidth(MAXIMUM_WINDOW_WIDTH);
    stage.setMaxHeight(MAXIMUM_WINDOW_HEIGHT);

    //Set window icon
    stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/favicon-96x96.png")));
    stage.setScene(scene);
    stage.show();

}

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      launch(args);
  }

}

非常感谢一个例子! 谢谢!

编辑:

这就是我要做的事情:应用程序从不透明度0.1开始淡入淡出。

这是我尝试过的,但它不起作用。

    public class MainApp extends Application {

    private final double MINIMUM_WINDOW_WIDTH = 500.0;
    private final double MINIMUM_WINDOW_HEIGHT = 400.0;
    private final double MAXIMUM_WINDOW_WIDTH = 800.0;
    private final double MAXIMUM_WINDOW_HEIGHT = 600.0;

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("MainApp.fxml"));

    Scene scene = new Scene(root);

    //Set window title
    stage.setTitle("My app");

    //Set minimum window size
    stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
    stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);

    //Set maximum window size
    stage.setMaxWidth(MAXIMUM_WINDOW_WIDTH);
    stage.setMaxHeight(MAXIMUM_WINDOW_HEIGHT);

    //Set window icon
    stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/favicon-96x96.png")));

    //Fade in transition
    FadeTransition ft = new FadeTransition(Duration.millis(3000), root);
    ft.setFromValue(0.1); 
    ft.setToValue(1.0);
    ft.play();

    stage.setScene(scene);
    stage.show();

}

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      launch(args);
  }

}

1 个答案:

答案 0 :(得分:0)

尝试使用TimeLine:

public class MainApp extends Application {

    private final double MINIMUM_WINDOW_WIDTH = 500.0;
    private final double MINIMUM_WINDOW_HEIGHT = 400.0;
    private final double MAXIMUM_WINDOW_WIDTH = 800.0;
    private final double MAXIMUM_WINDOW_HEIGHT = 600.0;

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = new VBox();
        root.setStyle("-fx-background-color: #000;");
        //FXMLLoader.load(getClass().getResource("MainApp.fxml"))

        Scene scene = new Scene(root);

        //Set window title
        stage.setTitle("My app");

        //Set minimum window size
        stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
        stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);

        //Set maximum window size
        stage.setMaxWidth(MAXIMUM_WINDOW_WIDTH);
        stage.setMaxHeight(MAXIMUM_WINDOW_HEIGHT);

        stage.setScene(scene);
        stage.show();

        //Fade in
        DoubleProperty opacity = root.opacityProperty();
        Timeline fadeIn = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                new KeyFrame(new Duration(4000), new KeyValue(opacity, 1.0))
        );
        fadeIn.play();
    }

    public static void main(String[] args) { launch(args); }
}