简体   繁体   English

JavaFX fxml 加载器无法正常工作

[英]JavaFX fxml loader does not work properly

testFX.java : testFX.java :

public class testFX extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/testFX/view/test.fxml"));
            System.out.println("after set location");
            //PROBLEM
            AnchorPane root = (AnchorPane)loader.load();
            System.out.println("Does not happen");
            testFXController listController = loader.getController();
            listController.start();
            Scene scene = new Scene(root, 200, 300);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        catch (Exception ex){
            System.out.println("Error");
        }
    }

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

testFXController.java : testFXController.java :

package testFX.view;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;

public class testFXController {
    @FXML ListView<String> listView;
    private ObservableList<String> obsList;

    public void start() {
        // create an ObservableList
        // from an ArrayList
        obsList = FXCollections.observableArrayList("Giants", "Patriots", "Jaguars");
        listView.setItems(obsList);
    }
}

test.fxml :测试.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.ListView?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="view.testFXController">
    <ListView fx:id="listView" 
        AnchorPane.topAnchor="10"
        AnchorPane.leftAnchor="10" 
        AnchorPane.rightAnchor="10"
        AnchorPane.bottomAnchor="10" />
</AnchorPane>

When I run the testFX.java, the system prints:当我运行 testFX.java 时,系统打印:

after set location  
Error

This is the professor's code and I cannot seem to get it running.这是教授的代码,我似乎无法运行它。 I realized that the main problem is in the line of code AnchorPane root = (AnchorPane)loader.load();我意识到主要问题在于代码行AnchorPane root = (AnchorPane)loader.load(); but I have no idea how to fix this, can someone help?但我不知道如何解决这个问题,有人可以帮忙吗?

The value fx:controller attribute is most likely wrong (unless you have a different controller class than the one posted)fx:controller属性很可能是错误的(除非您的控制器类与发布的控制器类不同)

The controller you want to use: testFX.view.testFXController您要使用的控制器: testFX.view.testFXController

Attribute value in the fxml: view.testFXController != testFX.view.testFXController fxml 中的属性值: view.testFXController != testFX.view.testFXController

Assuming there is no other error that cannot be reproduced with the information in the question, fixing the attribute value should work.假设没有其他错误不能用问题中的信息重现,修复属性值应该可以工作。

The fx:controller attribute within the fxml file is the culprit here. fxml 文件中的fx:controller属性是这里的罪魁祸首。

fx:controller="application.Controller" worked for me. fx:controller="application.Controller"为我工作。

Therefore, fx:controller="testFX.view.testFXController" should work for you.因此, fx:controller="testFX.view.testFXController"应该适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaFX:绑定不适用于包含的 FXML - JavaFX: Binding does not work with included FXML ListView无法使用JavaFX FXML正确更新 - ListView is not updating properly with JavaFX FXML 使用 FXML 加载器访问 JavaFX 场景中的 Canvas - Accessing a Canvas in JavaFX scene using FXML Loader 为什么此javafx fxml不调整大小? - Why is this javafx fxml does not resize? JavaFX 8:javafx.fxml.LoadException-fxmlLoader.load()不起作用,但fxmlLoader.getController()起作用 - JavaFX 8: javafx.fxml.LoadException - fxmlLoader.load() doesn't work, but fxmlLoader.getController() does Javafx/FXML:initialize() 方法和 FXML Loader 之间的冲突:Initialize: NullPointerException, FXML.LoadException - Javafx/FXML: conflict between initialize() method and FXML Loader:Initialize: NullPointerException, FXML.LoadException Javafx - On intelliJ with gradle error: package javafx.fxml does not exist import javafx.fxml.FXML - Javafx - On intelliJ with gradle error: package javafx.fxml does not exist import javafx.fxml.FXML FXML Loader不会创建新实例 - FXML Loader does not create new instance 默认情况下,JavaFX在哪里创建FXML文件? - Where does JavaFX create the FXML file by default? 这个 fxml 错误代码在 JavaFx 中意味着什么 - What does this fxml error code mean in JavaFx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM