gpt4 book ai didi

标签上的 JavaFX 淡入淡出转换没有执行任何操作

转载 作者:行者123 更新时间:2023-12-01 09:55:56 25 4
gpt4 key购买 nike

在此处输入代码我希望标签从不显示 (0.0) 转换为显示 (1.0)

    @FXML
private Label welcomeLabel;

public FadeTransition ft = new FadeTransition(Duration.millis(3000));


public void init(){

ft.setNode(welcomeLabel);

ft.setFromValue(0.0);
ft.setToValue(1.0);
ft.setCycleCount(1);
ft.setAutoReverse(false);

ft.play();

}

这是应用程序类

package com.ben.main;

公共(public)类应用程序扩展应用程序{

private Stage primaryStage;
private Scene loginScene;
LoginUIController loginUIController = new LoginUIController();

public void start(Stage primaryStage) {

this.primaryStage = primaryStage;

initApp();
loginUIController.init();

}

private void initApp() {

Parent root = null;

try {
root = FXMLLoader.load(getClass().getResource("loginUIFXML.fxml"));
} catch (IOException e){
System.err.println("There was an error... " + e);
}

loginScene = new Scene(root);

primaryStage.setTitle("project");
primaryStage.setResizable(false);
primaryStage.setScene(loginScene);
primaryStage.show();


}

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

}

我是否也必须将其添加到此处的场景中?我遇到了麻烦,目前只是输入一些内容来更新编辑内容。

最佳答案

FXML loading process for controllers通过反射起作用。它将调用名为 initialize() 的方法。它不会知道有关名为 init() 的方法的任何信息,因此永远不会调用该方法。因此,您应该将方法名称从 init() 更改为 initialize()

I call the init method after the FXML file is loaded.

是的,现在可以通过您添加到问题中的附加代码看到这一点。

但是,您在不是由 FXMLLoader 创建的 Controller 的新实例上调用 init,而不是在由 FXMLLoader 创建的 Controller 上调用 init。您使用 new 创建的 Controller 实例不以任何方式与场景图关联。

所以,不要这样做,不要使用 new 创建新的 Controller 。相反,使用由加载程序创建的 Controller 。

如果您想获取对 FXMLLoader 加载的 Controller 的引用,您应该从加载器获取它,如以下答案所示:

相关部分复制并粘贴如下:

FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"customerDialog.fxml"
)
);

Stage stage = new Stage(StageStyle.DECORATED);
stage.setScene(
new Scene(
(Pane) loader.load()
)
);

CustomerDialogController controller =
loader.<CustomerDialogController>getController();
controller.initData(customer);

只需根据您的代码调整上面的模式即可。

关于标签上的 JavaFX 淡入淡出转换没有执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37220370/

25 4 0