在 JavaFX FXML 应用程序中实现 FadeTransition
Implementing FadeTransition in JavaFX FXML application
我已经看到在 JavaFX 应用程序中实现淡入淡出,但如何在 FXML 应用程序中实现它?
Oracle 文档说应该这样做:
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 淡入到 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);
}
}
尝试使用 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); }
}
我已经看到在 JavaFX 应用程序中实现淡入淡出,但如何在 FXML 应用程序中实现它?
Oracle 文档说应该这样做:
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 淡入到 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);
}
}
尝试使用 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); }
}