「译」Maven 集成 JavaFX 8 以及 <fx:root> 问题探讨

上一篇文章探讨了使用 IntelliJ IDEA 创建 JavaFX 工程,进而开发了所需应用程序。更实际的情况是需要使用 Maven, Gradle 等进行项目的构建。本文探讨使用 Maven 构建集成 JavaFX 8 的可执行程序的方法,以及 <fx:root> 根节点问题。

1. Maven 构建的程序未集成 FXML 布局文件

使用 Maven 直接构建,在 compile 阶段, .class 文件均被复制到 target/classes/ 目录,而对于 .FXML 文件,则分如下情况:

  1. simple.fxml 文件位于 src/main/resources/ 目录中,在 compile 阶段,simple.fxml 会按照层级复制到 target/classes/ 目录中,执行:
getClass().getClassLoader().getResource("simple.fxml")
getClass().getResource("/simple.fxml")
  1. 为了方便使用,simple.fxml 文件位于其 Controller 的同级目录中,此时在 compile 阶段,simple.fxml 会被忽略掉,Maven 不会复制位于 src 目录下的任何资源文件,故需要采取其他策略,通过搜索 StackOverflow 发现了解决方法如下:

pom.xml 文件中添加如下 resource 插件即可解决问题:

<build>
    ...
    <resources>
        <resource>
            <filtering>false</filtering>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.fxml</include>
            </includes>             
        </resource>
    <resources>
    ...
</build>

此时所有的 .fxml 文件均会被完整复制到 src 下的同级目录。

使用 Maven 构建可执行 Jar 可使用通用方法,具体参考:镜像1镜像2

可执行 Jar 构建完毕后,在 Windows 平台下可以直接双击执行。

2. FXML 文件中,「fx:root」根节点问题探讨

为了更加方便灵活地使用自定义控件,更方便的集成 Controller 和 FXML 资源文件,以下内容对 StackOverflow 的一则回复进行翻译修改:

假设想要设计一个自定义控件:HBox 中包含 TextFieldButton,不使用 FXML 文件时,自定义控件设计如下:

public class MyComponent extends HBox {
    private TextField textField ;
    private Button button ;

    public MyComponent() {
        textField = new TextField();
        button = new Button();
        this.getChildren().addAll(textField, button);
    }
}

此时可对该自定义控件方便地设计逻辑代码。

若使用 FXML 文件时,如:

<HBox>
    <TextField fx:id="textField"/>
    <Button fx:id="button" />
</HBox>

此时 HBox 的 Controller 定义如下:

public class MyComponent extends HBox {

    @FXML
    private TextField textField ;

    @FXML
    private Button button ;

    public MyComponent() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("MyComponent.fxml"));
            loader.setController(this);
            HBox hbox = loader.load();
            this.getChildren().add(hbox);
        } catch (IOException exc) {
            // handle exception
        }
    }
}

此时该自定义控件为一个 HBox 包裹一个 HBox,子 HBox 才包含 TextFieldButton,所以无法实现开始时,纯代码方式的自定义控件设计。

而使用 <fx:root> 后,可指导 Controller 类作为「根节点」,避免了 HBox 嵌套 HBox 的情况。

FXML 文件设计如下:

<fx:root type="javafx.scene.layout.HBox">
    <TextField fx:id="textField" />
    <Button fx:id="button" />
</fx:root>

FXML 文件同时指明了根节点的类型,资源文件对应的 Controller 设计如下:

public class MyComponent extends HBox {

    @FXML 
    private TextField textField ;

    @FXML
    private Button button ;

    public MyComponent() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("MyComponent.fxml"));
            loader.setController(this);
            loader.setRoot(this);
            loader.load();
        } catch (IOException exc) {
            // handle exception
        }
    }
}

此时可实现开始时,纯代码方式的自定义控件设计。

3. 参考资料

  1. JavaFX and Maven: NullPointerException: Location is required

  2. How to understand and use <fx:root>, in JavaFX

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,864评论 25 709
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,116评论 6 342
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,319评论 4 61
  • 3月11日下午2点,由华严书院养正班义工老师、耘心学堂田老师策划并主持的华严书院乐音树下“莱雅琴•古琴”公益音乐会...
    耘心学堂阅读 3,833评论 0 0