当我在 fxml 文件中使用 fx:controller
时,一切正常,但如果用户控制 (fx:root
顶部标签)。
因此,Intellij 停止索引文件,并且在 Controller 文件中我收到警告,因为未分配变量。
<fx:root prefHeight="500.0" prefWidth="710.0" type="VBox" xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1">
<FlowPane fx:id="flow" columnHalignment="CENTER" hgap="25.0" vgap="25.0"/>
</fx:root>
如何在不使用 fx:controller
的情况下指定用于索引的 Controller 文件?
最佳答案
我找到了解决方案,首先我仍然放置 fx:controller
属性,但改为
public CustomControl() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomControl.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
我正在使用
public CustomControl() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomControl.fxml"));
loader.setRoot(this);
loader.setControllerFactory(theClass -> this);
loader.load();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
我只是将 FXMLLoader#setController
替换为 FXMLLoader#setControllerFactory
,现在加载程序已成功加载我的自定义控件
关于javafx Intellij IDEA 索引用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61805245/