无法在 Javafx 中找到 fxml 文件

Unable to find fxml file in Javafx

我目前正在尝试按照网络上的 Javafx 教程进行操作,但遇到了一些问题。 我有一个包含 3 个不同包的项目。 第一个是:ch.makery.address,它包含 Main 第二个是:ch.makery.model 目前是空的 第三个是:ch.makery.view 它包含 2 个不同的 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);
    }}`

下面是错误 returns :


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$launchApplication6(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$launchApplication13(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null4(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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$null9(Unknown Source)
    ... 1 more
Exception running application ch.makery.adress.MainApp

据我了解,定位 fxml 文件时似乎出现了问题。但我不明白为什么。 如果有人可以帮助我,那将是完美的 谢谢

(如果我的英语有任何错误,对不起,这不是我的母语)

如果你的 Mainch.makery.address 而你的 fxml 在 ch.makery.view 那么这是错误的:

view/RootLayout.fxml

因为它尝试从 ch.makery.address.view.

加载文件

尝试

../view/RootLayout.fxml

相反。 (PersonOverview 相同)

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