[英]Implementing FadeTransition in JavaFX FXML application
I have seen fade in implemented in JavaFX application, but how can I implement it in FXML application? 我已经看到在JavaFX应用程序中实现了淡入淡出,但是如何在FXML应用程序中实现它呢?
This is how oracle docs says it should be done: 这是oracle docs所说的应该做的:
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. 这就是我正在尝试做的事情:应用程序从不透明度0.1渐变为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); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.