简体   繁体   中英

Implementing FadeTransition in JavaFX FXML application

I have seen fade in implemented in JavaFX application, but how can I implement it in FXML application?

This is how oracle docs says it should be done:

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

This is the main that loads the application:

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);
  }

}

An example is very much appreciated! Thank you!

EDIT:

This is what I am trying to do: Application opens fading in from opacity 0.1 to 1.

This what I've tried, but it doesn't work.

    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);
  }

}

Try using 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); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM