有没有一种模块化JavaFX应用程序的方法? [英] Is there a way to modularize a JavaFX application?

查看:83
本文介绍了有没有一种模块化JavaFX应用程序的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始玩JavaFX 2了,我真的很喜欢使用FXML创建UI的便捷性.但是,一旦您了解了基本示例,并且需要具有许多窗口的UI,将整个应用程序UI的定义包含在单个FXML文件中似乎是不合逻辑的.

I've started toying with JavaFX 2 and I really like the ease with which one can create a UI with FXML. However, once you get past the basic examples and you need a UI with many windows, it seems illogical to have the definition for the whole application's UI in a single FXML file.

是否可以在单独的FXML文件中编写单独的组件,然后根据需要添加它们?举例来说,当用户单击主菜单中的某个项目时,我希望弹出一个窗口.我可以在单独的FXML文件中编写该窗口的定义,在运行时加载它并将其添加到节点树中吗?

Is it possible to write separate components in separate FXML files and then include them as needed? Say for example that I wanted a window to popup when the user clicks on an item from the main menu; could I write the definition for that window in a separate FXML file, load it at runtime and add it to the node tree?

推荐答案

从FXML角度来看,有两种方法:

From FXML perspective there are two approaches:

  1. 只需使用 fx:include 指令可将应用程序拆分为不同fxml文件中的逻辑部分.

  1. Simply use fx:include directive to split application into logical parts in different fxml files.

更高级的方法是将应用程序的各个部分实现为单独的控件".它将具有自己的API(可以非常简单)来提供数据,并且可以在FXML或FX API中使用,例如内置控件.有关如何使用FXML实施的详细信息,请参阅下一个问题:

More advanced way would be to implement parts of your application as separate "controls". Which will have their own (can be very simple) API to provide data and can be used in FXML or FX API like built-in controls. See next question for details how to implement that using FXML: How to pass object created in FXML Controller1 to Controller2 of inner FXML control

这篇关于有没有一种模块化JavaFX应用程序的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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