java - FXML getLocation() 返回 null 即使我认为我设置正确
问题描述
当我尝试将我的 FXML 工作表链接到我的主类时,我收到以下错误
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2463)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2439)
at com.nandha.client.start(client.java:28)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
这是我的启动方法
stage.setTitle("Client");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Main.fxml"));
System.out.println(loader.getLocation()); //Prints Null
VBox box1 = loader.<VBox>load(); //Code Breaks Here
Scene scene = new Scene(box1, 300, 150);
stage.setScene(scene);
stage.show();
}
这是我的 FXML 文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.Button?>
<VBox fx:controller="com.nandha.Controller" xmlns:fx="http://javafx.com/fxml">
<Text fx:id="status" text="Not Connected"/>
<Button fx:id="connect" text="Connect" onAction="#connectToServer"/>
<Button fx:id="buttonNew" text = "Add New Patient" onAction="#openCreateNewPage"/>
<Button fx:id="buttonUpdate" text="Existing Patient"/>
</VBox>
FXML 文件、主类和对应的类都在同一个文件夹和包中,src/com/nandha/。我尝试过在 main 前面使用和不使用斜杠,并添加包名。提前致谢!
编辑:由于某种原因,当我从 xml 文件中删除 fx:controller="com.nandha.Controller" 时,该位置开始工作,有人可以帮助解释为什么这会导致问题吗?
解决方案
推荐阅读
- python - python函数输入参数中的空格,因为它是一个cython函数?
- jenkins - 在多分支管道中禁用分支并使用扫描事件重新打开
- javascript - 查找具有重复项的两个数组中的差异
- java - Spring - 根据数据库决定配置文件
- microsoft-graph-api - 使用 Bot 向 Microsoft Teams 会议发送消息
- html - 如何导出 pdf 或发送包含包含边框的 html 的电子邮件?
- react-native - 在设置 redux 状态后呈现经过身份验证和未经身份验证的条件堆栈导航器
- python - 为什么滚动与我的网页抓取效果不佳?
- mqtt - 使用 MQTT 和 Solace 限制/保护主题
- javascript - javascript为谷歌地图latlng添加数组值lat和lng的键