javafx - 通过控制器完成 init 后的 JavaFX FadeTransition
问题描述
我有这个应用程序:
public class FOO extends Application {
private Scene scene;
@Override
public void start(Stage stage) throws Exception {
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void init() throws Exception {
URL resource = FXMLTabPaneController.class.getResource("FXMLTabPane.fxml");
System.out.println(resource);
Parent root = FXMLLoader.load(resource);
scene = new Scene(root);
}
}
这作为一个控制器:
public class FXMLTabPaneController implements Initializable {
@FXML
private TabPane tabPane;
@FXML
private AnchorPane anchorPane;
@Override
public void initialize(URL url, ResourceBundle rb) {
FadeTransition fadein = new FadeTransition(Duration.seconds(5), tabPane);
fadein.setFromValue(0);
fadein.setToValue(1);
fadein.play();
}
}
我想在应用程序启动后慢慢显示 Tabpane,但它从已经看到的应用程序 Tabpane 开始。
解决方案
尝试这个:
public class FXMLTabPaneController {
@FXML
private TabPane tabPane;
@FXML
private AnchorPane anchorPane;
@FXML
private void initialize() {
}
public void show(WindowEvent event) {
FadeTransition fadein = new FadeTransition(Duration.seconds(5), tabPane);
fadein.setFromValue(0);
fadein.setToValue(1);
fadein.play();
}
}
和 FOO 类
public class FOO extends Application {
private Scene scene;
private FXMLTabPaneController controller;
@Override
public void init() throws Exception {
super.init();
URL resource = FXMLTabPaneController.class.getResource("FXMLTabPane.fxml");
System.out.println(resource);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(resource);
Parent root = loader.load();
controller = loader.getController();
scene = new Scene(root);
}
@Override
public void start(Stage stage) throws Exception {
stage.setOnShown(controller::show);
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
推荐阅读
- python - 尝试记录警告时出现 KeyError
- node.js - 如何存储多个用户名并在聊天屏幕上显示?Socket.io 和 Node.js
- corrupt-data - .xlxs 文件在使用带有脚本任务的 SSIS 从谷歌驱动器下载时损坏
- tensorflow - 如何添加个人数据增强?
- css - React Boostrap 4 css模块未应用于导航栏
- javascript - 如何使用基于其他数组的嵌套对象数组过滤对象数组
- python - 将值从列表列表映射到字典的简单方法
- sql - 如何添加具有某种类型的数字序列的列
- c# - OleDbCommand 创建带括号的 excel 列名
- java - 在没有 POJO 的情况下将 JsonObject 反序列化(转换)为 DateTime