“类 javafx.scene.layout.BorderPane 无法转换为类 javafx.fxml.FXMLLoader”

问题描述 投票:0回答:1

我正在使用 JavaFX 创建登录表单。登录后,您输入的用户名将在屏幕上显示为“欢迎,用户名”。但是,当我单击登录按钮时,出现错误消息:

class javafx.scene.layout.BorderPane cannot be cast to class javafx.fxml.FXMLLoader (javafx.scene.layout.BorderPane is in module javafx.graphics of loader 'app'; javafx.fxml.FXMLLoader is in module javafx.fxml of loader 'app')

这是我的代码:

public class HelloApplication extends Application {
    private static Stage stg;

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

    @Override
    public void start(Stage stage) {
        try {
            stg = stage;
            stage.setResizable(false);
            Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
            stage.setTitle("Login");
            stage.setScene(new Scene(root, 600, 340));
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void changeSceneWelcome(String fxml, String username) {
        try {
            FXMLLoader loader = FXMLLoader.load(getClass().getResource(fxml));
            AfterLoginController afterLoginController = loader.getController();
            afterLoginController.welcomeName(username);
            Parent pane = loader.load();
            stg.setResizable(false);
            stg.setTitle("Welcome!");
            stg.setScene(new Scene(pane, 800, 600));
            stg.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

错误发生在

changeSceneWelcome
方法。

我应该怎么做才能修复这个错误?感谢您的阅读。

java exception javafx scenebuilder
1个回答
0
投票

父根 = FXMLLoader.load(resource) 这是错误的。 反而 var loader= FXMLLoader.load(资源); varparent=loader.load();

© www.soinside.com 2019 - 2024. All rights reserved.