【问题标题】:AnchorPane homePage = (AnchorPane) loader.load(); not workingAnchorPane 主页 = (AnchorPane) loader.load();不工作
【发布时间】:2016-07-18 12:28:11
【问题描述】:

下面是代码:

 @Override
 public void start(Stage primaryStage) {
     System.out.println("Enter start method");
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Bank Account App");
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/Overview.fxml"));
        System.out.println("Location is set");
        AnchorPane homePage = (AnchorPane) loader.load();

        OverviewController controller = loader.getController();
        controller.setMainApp(this);

        // Show the scene containing the root layout.
        Scene scene = new Scene(homePage);
        primaryStage.setScene(scene);
        primaryStage.show();
        System.out.println("Showed successfully");
        this.primaryStage.setWidth(800);
    } catch (IOException e) {
        System.out.println("Not Working");;
    }


 }

这是错误信息:

Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409``)
at Question2.Controller.MainApp.start(MainApp.java:40)

为什么AnchorPane homePage = (AnchorPane) loader.load(); 行不起作用? 我确信我的 fxml 文件将锚窗格作为其父级。

【问题讨论】:

标签: javafx fxmlloader


【解决方案1】:

请在屏幕的 FXML 文件的最高容器中添加以下开头行

fx:controller="com.fxtest.MyController"

com.fxtest.MyController 是你的类,其中包含类似的方法:

void buttonOnAction(ActionEvent event) throws IOException{

负责捕捉鼠标点击按钮...


您也可以在以下位置添加控制器方法:

SceneBuilder -> 属性(请求对象的)-> OnAction

【讨论】:

    【解决方案2】:

    检查MainApp.class.getResource("view/Overview.fxml") 实际返回的内容。

    【讨论】:

    • 这将 OP 指向正确的方向;这不是一个好的答案,因为它没有解释问题,但只是提供了“调试建议”。再多说 2 到 3 句话来解释这个问题就可以得到很好的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2021-12-09
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多