更新时间:2022-02-14 00:05:26
您的 MainController
没有零参数构造函数.如果 FXMLLoader
在根元素上遇到 fx:controller
属性,它会尝试通过(有效地)调用类的零参数构造函数来创建该控制器的实例在属性中指定.
Your MainController
doesn't have a zero-argument constructor. If the FXMLLoader
encounters a fx:controller
attribute on the root element, it attempts to create an instance of that controller by (effectively) calling the zero-argument constructor of the class specified in the attribute.
要解决此问题(最简单的方法),从 FXML 文件中删除 fx:controller
属性,并在 上手动"设置控制器FXMLLoader
.您需要创建一个 FXMLLoader
实例,而不是依赖于静态的 load(...)
方法:
To fix this (the simplest way), remove the fx:controller
attribute from the FXML file, and set the controller "by hand" on the FXMLLoader
. You need to create an FXMLLoader
instance instead of relying on the static load(...)
method:
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setController(new MainController(path));
Pane mainPane = loader.load();