gpt4 book ai didi

java - 在 Spring 中创建自定义 FXML 加载器时出错

转载 作者:行者123 更新时间:2023-12-01 12:43:04 31 4
gpt4 key购买 nike

我一直在尝试将我的 JavaFx 应用程序移动到 Spring 以利用依赖注入(inject)。该项目已经变得太大,无法在类之间手动移动项目。

该代码在我的 netbeans IDE 上没有显示任何红色错误突出显示,但当我运行时,我不断收到此错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController' defined in class wakiliproject.controllerinjection.SampleAppFactory: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public wakiliproject.controllerinjection.controllers.HomeController wakiliproject.controllerinjection.SampleAppFactory.homeController() throws java.io.IOException] threw exception; nested exception is javafx.fxml.LoadException: Base location is undefined.

我有这样的:

public class SampleApp extends Application {

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

public void start(Stage stage) throws Exception {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(SampleAppFactory.class);

Group box = new Group();
HomeController homeController = context.getBean(HomeController.class);
box.getChildren().add(homeController.getView());

Scene scene = new Scene(box, 600, 200);
stage.setScene(scene);
stage.setTitle("JFX2.0 Sprung");
stage.show();
}
}

--

@Configuration
public class SampleAppFactory {

@Bean
public HomeController homeController() throws IOException {
return (HomeController) loadController("/resources/fxml/Home.fxml");
}

protected Object loadController(String url) throws IOException {
InputStream fxmlStream = null;
try {
fxmlStream = getClass().getResourceAsStream(url);
FXMLLoader loader = new FXMLLoader();
loader.load(fxmlStream);
return loader.getController();
} finally {
if (fxmlStream != null) {
fxmlStream.close();
}
}
}
}

--

public class HomeController {

@FXML
private AnchorPane view;

public AnchorPane getView() {
return view;
}
}

FXML 文件的摘录如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<AnchorPane fx:id="view" prefHeight="654.0" prefWidth="900.0" styleClass="AnchorPane" visible="true" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="wakiliproject.controllerinjection.controllers.HomeController">
<children>
<Pane id="wrapperPane" prefHeight="600.0" prefWidth="700.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Pane id="top" layoutX="-1.0" layoutY="3.0" prefWidth="879.0" styleClass="appNav" visible="true">
<children>
<HBox id="HBox" alignment="CENTER" layoutX="12.0" layoutY="6.0" spacing="0.0">
<children>
<Label prefHeight="32.0" prefWidth="32.0" styleClass="titleBarHome" />
<Label prefHeight="32.0" prefWidth="32.0" styleClass="titleBarPublisher" />
</children>
</HBox>

最佳答案

看来您需要在 FXMLLoader 上设置 FXML 文件的位置。例如,如果您的 FXML 文件需要解析相对位置,则可能会出现这种情况。使用 FXMLLoader.load(URL) 方法不会设置位置。尝试一下

protected Object loadController(String url) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource(url));
loader.load();
return loader.getController();
}

而不是您当前的实现。

顺便问一下,你看过afterburner.fx吗?作为 JavaFX 中依赖注入(inject)的 Spring 的(更轻量级的)替代品?

关于java - 在 Spring 中创建自定义 FXML 加载器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24909935/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com