簡體   English   中英

FFX階段加載時的JavaFX 8問題

[英]JavaFX 8 Problems with FXML loading as stage

我正在嘗試在JavaFX 8中作為階段加載FXML文件。這是我更新的代碼

Stage window;
private AnchorPane GuiStage;

    public void showMainView() throws IOException
    {
    try{
       FXMLLoader loader = new   MLLoader(MyViewPart.class.getResource("/bthesis/views/test.fxml"));
       AnchorPane GuiStage = loader.load();
       Scene s1 = new Scene(GuiStage);
       window.setScene(s1);
       window.show();
    }catch(Exception e){
        System.out.println(e);
    }
    }

我的FXML文件的完整補丁是

C:\Users\Łukasz\workspace\BThesis\src\bthesis\views\test.fxml

包裹名字

bthesis.views

我在我的FXML文件中添加了fx:controller,現在出現空指針錯誤。 完整的錯誤跟蹤。

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(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    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(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at bthesis.MyViewPart.showMainView(MyViewPart.java:67)
    at bthesis.MyViewPart.start(MyViewPart.java:102)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    ... 1 more
Exception running application bthesis.MyViewPart

使用setLocation方法時,您需要像這樣更改路徑

 loader.setLocation(MyViewPart.class.getResource("views/test.fxml"));

如果這沒有幫助。 在視圖包中創建一個Java類(例如Helper) ,然后將其引用用作

  loader.setLocation(Helper.class.getResource("test.fxml"));

我認為您在設定路徑方面有些困惑。 我解釋。

If you project directory structure is like

    src
    .
    .
    .----bthesis
         .
         .
         .------views
                .
                .
                .test.fxml

然后,您必須像這樣設置位置路徑。

loader.setLocation(MyViewPart.class.getResource("/bthesis/views/test.fxml"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM