2017-02-20 30 views
0

我正在關注PluralSite上的這個課程,我正在複製代碼,但是他使用的是NetBeans,而我正在使用eclipse,而且我無法弄清楚發生了什麼事情。使用netbeans和ecplise使用fxml文件時有什麼不同嗎?我會appriciate如果有人可以幫助。如果您還需要更多信息,請通過一切方式詢問。我是javafx的新手,所以很好。該FXML文件是在同一個包爲別人:FXML文件導致應用程序無法打開

這裏是例外,我得到的,其次是應用程序代碼,然後將FXML代碼

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$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.NullPointerException: Location is required. 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at application.SimpleUI.start(SimpleUI.java:18) 
    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) 
    ... 1 more 
Exception running application application.SimpleUI 





    //first is my app 
    package application; 

    import java.io.IOException; 

    import javafx.application.Application; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.Label; 
    import javafx.scene.control.TextField; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.layout.VBox; 
    import javafx.stage.Stage; 

    public class SimpleUI extends Application { 
     @Override 
     public void start(Stage primaryStage) throws Exception { 
      HBox box =  FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml")); 

    //  VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down")); 
    // 
    //  box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox); 

      Scene scene = new Scene(box); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } 

     public static void main(String[] args) { 
      launch(args); 
     } 
    } 



    //this is my fxml code 
    import java.io.IOException; 

    import javafx.application.Application; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.Label; 
    import javafx.scene.control.TextField; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.layout.VBox; 
    import javafx.stage.Stage; 

    public class SimpleUI extends Application { 
     @Override 
     public void start(Stage primaryStage) throws Exception { 
      HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml")); 

    //  VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down")); 
    // 
    //  box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox); 

      Scene scene = new Scene(box); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } 

     public static void main(String[] args) { 
      launch(args); 
     } 
    } 




<?xml version="1.0" encoding="UTF-8"?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import java.scene.*?> 
<?import java.scene.control.*?> 
<?import java.scene.layout.*?> 

<?import javafx.scene.layout.*?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.TextField?> 

<HBox> 
    <children> 
     <Label text= "UserName" /> 
     <TextField /> 
     <Button text= "Connect" /> 
    </children> 
</HBox> 

固定:我固定它感謝誰回答我的問題

+0

當你運行你的代碼時會發生什麼?它會給出錯誤還是什麼? – TM00

+0

是的,我編輯了問題,以便你可以看到異常,將看到這個問題,我看到,也許fxml文件是在錯誤的目錄中,但它不是 –

+0

顯示您的項目文件結構。如果'simpleui.fxml'與'SimpleUI'類在同一個包中,那麼'FXMLLoader.load(getClass()。getResource(「simpleui.fxml」));'將正確加載FXML文件。 – MBec

回答

0

我不知道這將解決你的問題,但一個差我我的裝載FXML文件典型的方式和你貼的代碼之間看到的是路徑的人在你裝載機即:

 HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml")); 

我normaly加載FXML這樣:

 AnchorPane page = (AnchorPane) FXMLLoader.load(PreloadController.class.getResource("PreloaderGUI.fxml")); 

,並確保我Controller和FXML文件是在同一個包。我假設這些示例中getResource方法是相同的,但String傳遞的路徑是不同的。嘗試並將其更改爲fxml文件的名稱,然後查看它是否有效。即如下:

HBox box = FXMLLoader.load(getClass().getResource("simpleui.fxml")); 
+0

FXML和Controller不必在同一個包裏。 'Controller'的位置從FXML文件中的fx:controller =「path.to.Controller」獲得。加載FXML文件與從類路徑加載任何資源相同。請參閱:http://stackoverflow.com/a/2593175/6708868 – MBec

+0

仍不起作用 –

相關問題