如何在资源文件夹中引用 javafx fxml 文件? [英] How to reference javafx fxml files in resource folder?

查看:36
本文介绍了如何在资源文件夹中引用 javafx fxml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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.

编辑

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

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

但我还是有错误.

推荐答案

通用资源查找信息

这个答案讨论了 FXML 位置查找,它实际上只是 Java 中通用资源查找任务的一个子集.资源位置由调用程序作为输入传递给 FXMLLoader,因此资源查找本身是调用应用程序代码的一部分,而不是 FXMLLoader.

This answer discusses FXML location lookup, which is really just a subset of the generic resource lookup task in Java. The resource location is passed by the calling program as input to the FXMLLoader, so the resource lookup itself is part of your calling application code and not the FXMLLoader.

要全面了解 Java/JavaFX 应用程序的通用资源信息(包括推荐的故障排除步骤),请参阅:

For thorough coverage of generic resource information (including recommended troubleshooting steps) for Java/JavaFX applications, please refer to:

示例用法

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

位置解析选项

  1. 将所有 fxml 放在 src/main/resources 目录中.

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

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

  • 将所有 fxml 放在 src/main/resources/fxml 目录中.

  • Put all of your fxml in a src/main/resources/fxml directory.

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

  • 将fxml放在对应的资源目录下;例如src/main/resources/com/mycompany/myapp.

  • Place fxml in a corresponding resource directory; e.g. src/main/resources/com/mycompany/myapp.

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

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

    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.

    FXMLLoader 使用建议

    1. 通过 new FXMLLoader() 实例化一个 FXMLLoader,而不是使用静态方法在 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.

    设置位置 在实例化的 FXMLLoader 上并调用load() 而不是使用从流加载加载(流).

    Set the location on the instantiated FXMLLoader and call load() rather than loading from a stream using load(stream).

    • 在加载器上设置基于 URL 的位置允许解析在 fxml 和 css 文件中加载的相关资源.相对的资源无法解析基于流的构造函数.

    要根据类派生位置,请使用getClass().getResource(),因为它是基于 URL 的,而不是getClass().getResourceAsStream() 这是基于流的.

    To derive a location based upon a class, use getClass().getResource(), as it is URL based, rather than getClass().getResourceAsStream() which is stream based.

    IDE 和构建设置

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

    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.

    Java Jigsaw 模块化应用程序的注释

    见:

    具体来说,不要写:

    ComboBoxStyling.class.getClassLoader()
        .getResource("/css/styleclass.css");
    

    改为写:

    ComboBoxStyling.class
        .getResource("/css/styleclass.css"); 
    

    也就是说,直接从类中获取资源,而不是从类加载器中获取.如果您从类加载器而不是类中获取资源,那么基于模块配置的查找会有额外的限制.这些限制可能很难理解,请参阅 ClassClassLoader getResource 方法获取信息(如果需要).

    That is, get the resource from the class directly rather, NOT from the class loader. If you get resources from a class loader rather than the class, then there are additional restrictions on lookup based upon the module configuration. These restrictions can be hard to understand, see the documentation for the Class and ClassLoader getResource methods for information if needed.

    这篇关于如何在资源文件夹中引用 javafx fxml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆