如何在资源文件夹中引用 javafx fxml 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19602727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:41:14  来源:igfitidea点击:

How to reference javafx fxml files in resource folder?

javaresourcesjavafxfxmlbuildpath

提问by j will

I am creating a javafx GUI application and my project is a maven configured project. I want to be able to reference my fxml files like this in my controllers:

我正在创建一个 javafx GUI 应用程序,我的项目是一个 maven 配置的项目。我希望能够在我的控制器中像这样引用我的 fxml 文件:

FXMLLoader.load(getClass().getResource("main.fxml"); 

Where my main.fxml file is located in the src/main/resources folder and my controller is in the src/main/java folder. How do i go about doing this? My src/main/resources folder is in the build path and i am able to call a .properties file that is in the src/main/resources folder from a class in the src/main/java folder.

我的 main.fxml 文件位于 src/main/resources 文件夹中,而我的控制器位于 src/main/java 文件夹中。我该怎么做?我的 src/main/resources 文件夹位于构建路径中,我能够从 src/main/java 文件夹中的类调用位于 src/main/resources 文件夹中的 .properties 文件。

Edit

编辑

I attempted to place the fxml file in the corresponding directory of the main resources folder:

我试图将 fxml 文件放在主资源文件夹的相应目录中:

enter image description here

在此处输入图片说明

but i still got an error.

但我仍然遇到错误。

采纳答案by jewelsea

Example usage

示例用法

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/main.fxml"));
Parent content = loader.load(); 

Location resolution options

位置解析选项

  1. Put all of your fxml in the src/main/resources directory.

    loader.setLocation(getClass().getResource("/main.fxml"));
    
  2. Put all of your fxml in a src/main/resources/fxml directory.

    loader.setLocation(getClass().getResource("/fxml/main.fxml"));
    
  3. Place fxml in a corresponding resource directory; e.g. src/main/resources/com/mycompany/myapp.

    loader.setLocation(getClass().getResource("main.fxml")); 
    
  1. 将所有 fxml 放在 src/main/resources 目录中。

    loader.setLocation(getClass().getResource("/main.fxml"));
    
  2. 将所有 fxml 放在 src/main/resources/fxml 目录中。

    loader.setLocation(getClass().getResource("/fxml/main.fxml"));
    
  3. 将fxml放在对应的资源目录下;例如 src/main/resources/com/mycompany/myapp。

    loader.setLocation(getClass().getResource("main.fxml")); 
    

The last option assumes that the class from which you are loading the fxml is in the same relative location in the corresponding Java source hierarchy. For example, you might invoke the last load command from source com.mycompany.myapp.Main.java.

最后一个选项假定您从中加载 fxml 的类在相应的 Java 源代码层次结构中处于相同的相对位置。例如,您可以从 source 调用最后一个加载命令com.mycompany.myapp.Main.java

FXMLLoader usage recommendations

FXMLLoader 使用建议

  1. Instantiate an FXMLLoader via new FXMLLoader()rather than using the static methods on the FXMLLoader.

    • The static methods become confusing when you want to get values(like instantiated controllers) out of a loader.
  2. Set the locationon the instantiated FXMLLoader and call load()rather than loading from a stream using load(stream).

    • Setting a URL based location on the loader allows for resolution of relative resources loaded in fxml and css files. Relative resources do not resolve for a stream based constructor.
  3. To derive a location based upon a class, use getClass().getResource(), as it is URL based, rather than getClass().getResourceAsStream()which is stream based.

  1. 通过new FXMLLoader()而不是使用FXMLLoader 上的静态方法实例化FXMLLoader

    • 当您想从加载器中获取值(如实例化控制器)时,静态方法会变得混乱。
  2. 在实例化的 FXMLLoader 上设置位置并使用 调用 load()而不是从流加载 load(stream)

    • 在加载器上设置基于 URL 的位置允许解析在 fxml 和 css 文件中加载的相关资源。相对资源不解析基于流的构造函数。
  3. 要根据类派生位置,请使用 getClass().getResource(),因为它是基于 URL 的,而不是 getClass().getResourceAsStream()基于流的。

IDE and Build Settings

IDE 和构建设置

Ensure that your IDE or build tool is copying the fxml files from the resource directory to the build output directory. For understanding Intellij settings for this, see: How to convert a normal java project in intellij into a JavaFx project.

确保您的 IDE 或构建工具正在将 fxml 文件从资源目录复制到构建输出目录。要了解 Intellij 设置,请参阅:如何将 intellij 中的普通 Java 项目转换为 JavaFx 项目

回答by Dil

You are using FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource("main.fxml") );to load your main.fxml. This requires the main.fxml and the java class loading it in same directory/path. If you want to load an fxml file from a path/location different from the java class loading it, you need to use relative path. As @jewelsea mentioned above, you may use the relative path using / character before your main.fxml. So the code that will make it work for you in your case is

您正在使用FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource("main.fxml") );加载您的 main.fxml。这需要 main.fxml 和 java 类将它加载到相同的目录/路径中。如果要从与加载它的 java 类不同的路径/位置加载 fxml 文件,则需要使用相对路径。正如上面提到的@jewelsea,您可以在 main.fxml 之前使用 / 字符使用相对路径。所以在你的情况下,让它为你工作的代码是

FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource("/main.fxml") );

回答by Hendrik Ebbers

Open your resources folder in the file explorer of your operation system. Here you will see that you have created a "dominion.application" folder instead of an "application" folder inside of a "dominion" folder. Because of this the "MainController" class is placed in a different package as the fxml file.

在操作系统的文件资源管理器中打开资源文件夹。在这里,您将看到您在“dominion”文件夹中创建了一个“dominion.application”文件夹,而不是“application”文件夹。因此,“MainController”类与 fxml 文件放在不同的包中。