简体   繁体   中英

JavaFX wont load FXML, can't find resources

I'm trying to get my FXML files to load as UI elements from subfolders in this project structure:

InventoryManagementUI 
    InventoryManagementUI.Java

    Controller
        MainMenuController.Java
    View
        MainMenu.fxml
    Model

I'm having trouble getting the actual resources by location and have tried the following:

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"));

Here is my error log specifically when I use ...getResource("/View/MainMenu.fxml"):

These attempts are made within the start method of my InventoryManagementUI.java file. Why am I not able to get a reference to the 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)

If I attempt to load via full classpath (this should be the correct way) like so:

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

I get the error:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM