首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaFX无法加载FXML,找不到资源

JavaFX无法加载FXML,找不到资源
EN

Stack Overflow用户
提问于 2018-10-23 05:35:23
回答 1查看 458关注 0票数 1

我正在尝试将我的FXML文件作为UI元素从此项目结构的子文件夹中加载:

代码语言:javascript
复制
InventoryManagementUI 
    InventoryManagementUI.Java

    Controller
        MainMenuController.Java
    View
        MainMenu.fxml
    Model

我在按位置获取实际资源时遇到了问题,并尝试了以下方法:

代码语言:javascript
复制
Parent root = FXMLLoader.load(getClass().getResource("View/MainMenu.fxml"));        
Parent root = FXMLLoader.load(getClass().getResource("/View/MainMenu.fxml"));       
Parent root = FXMLLoader.load(getClass().getResource("inventorymanagementui/View/MainMenu.fxml"));
Parent root = FXMLLoader.load(getClass().getResource("/inventorymanagementui/View/MainMenu.fxml"));

以下是我使用...getResource(“/View/MainMenu.fxml”)时的错误日志:

这些尝试是在我的InventoryManagementUI.java文件的start方法中进行的。为什么我无法获取对FXML的引用

代码语言:javascript
复制
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at inventorymanagementui.InventoryManagementUI.start(InventoryManagementUI.java:69)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)

如果我尝试通过完整的类路径加载(这应该是正确的方式),如下所示:

代码语言:javascript
复制
...getResource("/inventorymanagementui/View/MainMenu.fxml"));

我得到了错误:

代码语言:javascript
复制
javafx.fxml.LoadException:file:...NetBeansProjects/InventoryManagementUI/dist/run2552658/InventoryManagementUI.jar!/inventorymanagementui/View/MainMenu.fxml:12

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at inventorymanagementui.InventoryManagementUI.start(InventoryManagementUI.java:69)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: inventorymanagementui.MainMenu.MainMenuController
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 17 more
EN

回答 1

Stack Overflow用户

发布于 2018-10-23 09:41:21

事实证明,当我重构代码时,我忘记了将fx:id声明从前面的inventorymanagementui.MainMenu.MainMenuController更改为inventorymanagementui.Controller.MainMenuController

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52938082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档