gpt4 book ai didi

java - 当我将 Controller 放入嵌套 FXML 文件中时出现 LoadException

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:09 28 4
gpt4 key购买 nike

应用程序工作正常,直到我将 fx:controller 属性放入嵌套的 fxml 文件中。

主类:

@SpringBootApplication
public class MyApplication extends Application{

private Stage primaryStage;
private AnchorPane rootLayout;

public static void main(String[] args) {

SpringApplication.run(MyApplication.class, args);
launch(args);
}

@Override
public void start(Stage primaryStage) {

this.primaryStage = primaryStage;
this.primaryStage.setTitle("Survey Creator");
initRootLayout();
}

private void initRootLayout() {
try {
String pathToCss = "survey-generator/out/production/classes/css/Default.css";
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MyApplication.class.getResource("/view/MainLayout.fxml"));
rootLayout = loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
scene.getStylesheets().add(pathToCss);
primaryStage.setResizable(false);
primaryStage.show();
}

catch(Exception e){
throw new RuntimeException(e);
}
}
}

ClientController 类:

import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;


public class ClientController {
@FXML
private ComboBox<String> clientList;

@FXML
public void initialize(){
clientList = null;
}

public ClientController(ComboBox<String> clientList) {
this.clientList = clientList;
}
}

FXML:

<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" prefHeight="475.0" prefWidth="923.0"
fx:controller="com.surveycreator.controllers.MainController">
<children>
<fx:include source="MyText.fxml"/>
<VBox alignment="CENTER" layoutX="134.0" layoutY="48.0" prefHeight="100.0" prefWidth="668.0">
<children>
<fx:include source="ClientComboBox.fxml" fx:id="clientList"/>
//more stuff below

ClientCombo.fxml - 工作正常,直到我添加 "fx:controller="com.app.controllers.ClientController"

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

<?import javafx.scene.control.ComboBox?>

<ComboBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" prefHeight="25.0" prefWidth="519.0"
promptText="Select Client"/>

将上述 Controller 添加到 ClientCombo.fxml 后,出现以下错误:

Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: 
/C:/Users/myusername/IdeaProjects/survey-generator/out/production/classes/view/ClientComboBox.fxml:7
/C:/Users/myusername/IdeaProjects/survey-generator/out/production/classes/view/MainLayout.fxml:18

at com.app.MyApplication.initRootLayout(MyApplication.java:45)
at com.app.MyApplication.start(MyApplication.java:28)

最佳答案

您没有在 spring 中管理 fxml 上下文,bootifying 应该将其委托(delegate)给 spring,因此请尝试使用此

@SpringBootApplication
public class AirQualityFxApplication extends Application {

private ConfigurableApplicationContext context;
private Parent rootNode;

@Override
public void init() throws Exception {
SpringApplicationBuilder builder = new SpringApplicationBuilder(AirQualityFxApplication.class);
context = builder.run(getParameters().getRaw().toArray(new String[0]));

FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setControllerFactory(context::getBean);
rootNode = loader.load();
}


@Override
public void start(Stage primaryStage) throws Exception {
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
double width = visualBounds.getWidth();
double height = visualBounds.getHeight();

primaryStage.setScene(new Scene(rootNode, width, height));
primaryStage.centerOnScreen();
primaryStage.show();
}

@Override
public void stop() throws Exception {
context.close();
}
}

关于java - 当我将 Controller 放入嵌套 FXML 文件中时出现 LoadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52677262/

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