在javafx中加载来自另一个包的FXML文件并出现错误?

时间:2016-10-16 06:16:12

标签: java eclipse javafx fxml

我在从不同的packge加载fxml文件时遇到问题。

项目结构
This is a image of my project and packges

加载FXML文件PlayArea.java的类看起来像这样:

package controller;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author badar
 */
public class PlayArea extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/gui/FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

我的fxml文件是FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane fx:id="boderpane" minHeight="600.0" minWidth="600.0" prefHeight="600.0" prefWidth="600.0"  xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.FXMLDocumentController">
   <left>
      <JFXDrawer fx:id="drawer" defaultDrawerSize="200.0" prefHeight="503.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </left>
   <top>
      <HBox nodeOrientation="LEFT_TO_RIGHT" style="-fx-background-color: #42A5F5;" BorderPane.alignment="CENTER">
         <children>
            <JFXHamburger fx:id="hamburger" prefHeight="100.0" prefWidth="100.0" />
         </children>
      </HBox>
   </top>
</BorderPane>

修改

错误消息:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/home/badar/Documents/GitHub/DSA%20Lab%20Tasks/Playe%20Area/bin/gui/FXMLDocument.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    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 controller.PlayArea.start(PlayArea.java:22)
    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.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    ... 1 more
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 controller.FXMLDocumentController.initialize(FXMLDocumentController.java:65)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception running application controller.PlayArea

4 个答案:

答案 0 :(得分:3)

问题在于你的这一行

  Parent root = FXMLLoader.load(getClass().getResource("/gui/FXMLDocument.fxml"));

只需用

替换它
  Parent root = `FXMLLoader.load(getClass().getResource("../gui/FXMLDocument.fxml"));`

如果你仍然得到错误 在你的软件包的外部创建一个文件,即在src文件夹中说 Main.class

现在使用此

FXMLLoader.load(Main.class.getResource("gui/FXMLDocument.fxml"));

答案 1 :(得分:1)

如果您右键单击并打开您将找到属性的文件,您可以在fxml文件中设置控制器,并且您将有一个名为controller的标签,您可以键入确切的文件夹路径。

FXML EG

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication.FXMLController">

</AnchorPane>

答案 2 :(得分:1)

更新上一个答案

这是尝试过的,它工作正常

Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
                Scene scene = new Scene(root, 300, 275);            
                stage.setTitle("FXML Welcome");
                stage.setScene(scene);
                stage.show();

My Test Project Structure

希望这能解决问题

答案 3 :(得分:0)

我必须更改2行才能从另一个数据包中的控制器正确打开FXML文件:

root = loader.load(Main.class.getResource("document.fxml").openStream());
scene.getStylesheets().add(Main.class.getResource("application.css").toExternalForm());

存在Main.class而不是getClass()。