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

查看:230
本文介绍了如何在资源文件夹中引用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 /资源文件夹和我的控制器在src / main / java文件夹中。我该怎么做?我的src / main / resources文件夹在构建路径中,我能够调用src中的.properties文件来自src / main / java文件夹中的类的/ main / resources文件夹。

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:

但我仍然遇到错误。

推荐答案

示例用法

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 /主/资源/ 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. 通过新FXMLLoader()实例化FXMLLoader 而不是使用
      FXMLLoader上的静态方法

    • The static methods become confusing when you want to get values (like instantiated controllers) out of a loader.

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

    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.

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

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