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

时间:2018-10-22 21:35:23

标签: java javafx fxml

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

InventoryManagementUI 
    InventoryManagementUI.Java

    Controller
        MainMenuController.Java
    View
        MainMenu.fxml
    Model

我无法按位置获取实际资源,并尝试了以下操作:

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的引用

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)

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

...getResource("/inventorymanagementui/View/MainMenu.fxml"));

我得到了错误:

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

1 个答案:

答案 0 :(得分:0)

事实证明,当我重构代码时,我忘记将fx:id声明从以前的库存管理ui.MainMenu.MainMenuController更改为库存管理ui.Controller.MainMenuController