2015-08-31 86 views
1

我目前正在尝试关注网络上的Javafx教程,并且遇到一些问题。 我有一个项目有3个不同的包。 第一个是:ch.makery.address,它包含Main 第二个是:ch.makery.model目前为空 第三个是:ch.makery.view它包含两个不同的fxml文件,它们对应两个不同的布局。 下面是主要代码:无法在Javafx中找到fxml文件

`package ch.makery.address; 

import java.io.IOException; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class MainApp extends Application { 

    private Stage primaryStage; 
    private BorderPane rootLayout; 

    @Override 
    public void start(Stage primaryStage) { 
     this.primaryStage = primaryStage; 
     this.primaryStage.setTitle("AddressApp"); 

     initRootLayout(); 

     showPersonOverview(); 
    } 

    /** 
    * Initializes the root layout. 
    */ 
    public void initRootLayout() { 
     try { 
      // Load root layout from fxml file. 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml")); 
      rootLayout = (BorderPane) loader.load(); 

      // Show the scene containing the root layout. 
      Scene scene = new Scene(rootLayout); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Shows the person overview inside the root layout. 
    */ 
    public void showPersonOverview() { 
     try { 
      // Load person overview. 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml")); 
      AnchorPane personOverview = (AnchorPane) loader.load(); 

      // Set person overview into the center of root layout. 
      rootLayout.setCenter(personOverview); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Returns the main stage. 
    * @return 
    */ 
    public Stage getPrimaryStage() { 
     return primaryStage; 
    } 

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

这里是错误的,它返回:


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$156(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.IllegalStateException: Location is not set. 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.load(Unknown Source) 
    at ch.makery.adress.MainApp.initRootLayout(MainApp.java:35) 
    at ch.makery.adress.MainApp.start(MainApp.java:22) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(Unknown Source) 
    ... 1 more 
Exception running application ch.makery.adress.MainApp 

对于我所理解的是,它似乎是当问题查找fxml文件。但我不明白为什么。 如果有人能帮助我的话,这将是完美的 感谢

(很抱歉,如果有我的英文任何错误这不是我的母语)

回答

1

如果您Mainch.makery.addressch.makery.view您FXML那么这是错误的:

view/RootLayout.fxml 

因为它试图从ch.makery.address.view加载文件。

尝试

../view/RootLayout.fxml 

代替。 (与PersonOverview相同)

+0

那么我该如何改变它?我应该改变它(“RootLayout.fxml”)吗? – David

+0

似乎它解决了这个问题,但显然它不是唯一的...它仍然无法正常工作。 – David

+0

也许你应该采取更好的教程;-) – Kai

1

这是通向fxml文件的路径,该教程中的错误文件具有相同的问题。使用../向上移动一个目录应该可以修复路径到fxml文件。