首页 > 解决方案 > 将 fxml 文件加载到 BorderPane

问题描述

如何将 fxml 文件加载到边界窗格中?我想像这样加载它并且它不工作,你可以看到我在代码下面出现的错误

FXMLLoader loader2 = new FXMLLoader();
    path = getClass().getResource("CompanyHome.fxml");
    try {
    loader2.setLocation(path);            
    //whiteBorderPane = (BorderPane) loader2.load();
    whiteBorderPane.setCenter(loader2.load());
    } catch (IOException e){
        System.out.println("Not found: " + path);
        e.printStackTrace();
    }     

错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
    at gui.CompanyMainC.initializeView(CompanyMainC.java:90)
    at gui.CompanyMainC.start(CompanyMainC.java:70)
   at 
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
   Exception running application gui.CompanyMainC

如果有人知道在 dorderpane 中加载 fxml 文件的另一种方法,我会很高兴

标签: javafx

解决方案


推荐阅读