[英]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.