简体   繁体   English

我们应该在JavaFX自定义控件中使用FXML吗?

[英]Should we use FXML in JavaFX custom controls or not?

It seems wired to ask this, I would think that using FXML to write our custom components is the obviously the right way to go. 似乎有点问这个,我认为使用FXML来编写我们的自定义组件显然是正确的方法。

But as we can see from ControlsFX, JFXextras and even the book 'Mastering JavaFX8 controls do not use or mention the use of FXML in custom controls. 但正如我们从ControlsFX,JFXextras甚至“掌握JavaFX8控件”这本书中看到的那样,不要在自定义控件中使用或提及FXML的使用。

Despite that, the official documentation to say to go on that route, and create JavaFX controls via FXML. 尽管如此,官方文档说要继续这条路线,并通过FXML创建JavaFX控件。

What is the more correct way and why ? 什么是更正确的方式和原因?

There are two kinds of custom controls in JavaFX: JavaFX中有两种自定义控件:

  • fx:root based custom controls : These custom controls are styleable (support CSS), but are not skinnable. fx:基于root的自定义控件 :这些自定义控件是可定制的(支持CSS),但不是可换肤的。

    If you're an application developer, this is what you want to use most of the time. 如果您是应用程序开发人员,那么这就是您希望在大多数时间使用的内容。

    This kind of controls are usually application or organization specific. 这种控件通常是应用程序或组织特定的。 Additional custom skins for these controls are not needed, CSS is enough. 不需要为这些控件提供额外的自定义皮肤,CSS就足够了。

    Usually they just group some controls together and provide a nicer API and hide the internals. 通常他们只是将一些控件组合在一起并提供更好的API并隐藏内部。

    They are much easier to write than skinnable custom controls. 它们比可换肤的自定义控件更容易编写。

    I'm thinking of this as an alternative of the JPanel subclasses in the Swing-world. 我认为这是Swing-world中JPanel子类的替代品。

    I suggest to break down your GUI into small self-contained fx:root based controls (high cohesion, low coupling). 我建议将你的GUI细分为小型自包含的fx:基于root的控件(高内聚,低耦合)。 Use the Mediator Pattern to combine these controls (the parent control manages/ configures its child controls and listens to the events of its child controls. The child controls don't know about each other). 使用Mediator Pattern来组合这些控件(父控件管理/配置其子控件并侦听其子控件的事件。子控件彼此不了解)。

    You can create whole hierarchies of such controls. 您可以创建此类控件的整个层次结构。 Breaking them down into small controls will make maintenance easier (eg if the layout or the user interaction has to change). 将它们分解为小控件将使维护更容易(例如,如果布局或用户交互必须改变)。

  • skinnable custom controls : skinnable custom controls are not only styleable (via CSS) but their look and feel can be changed completely by providing a custom skin. skinnable自定义控件 :skinnable自定义控件不仅可以设置样式(通过CSS),而且可以通过提供自定义外观完全更改其外观。

    If you're a controls library developer then most likely you want to provide skinnable custom controls to allow the users of the library to get most out of JavaFX. 如果您是一个控件库开发人员,那么很可能您希望提供可换肤的自定义控件,以允许库的用户充分利用JavaFX。

    This kind of controls are usually very basic, highly reusable controls and often (but not always) use some custom painting as well. 这种控件通常是非常基本的,高度可重复使用的控件,并且通常(但不总是)使用一些自定义绘画。

    They are harder to get right than fx:root based custom controls. 他们比fx更难做到:基于root的自定义控件。

There is no correct way. 没有正确的方法。 But we may break it down to this: 但我们可以将其分解为:

Do you know the amount of JavaFX elements to display before your application has started? 您知道应用程序启动之前要显示的JavaFX元素数量吗? If yes, you can build your GUI with SceneBuilder or directly in XML writing the FXML File. 如果是,您可以使用SceneBuilder构建GUI,或者直接在XML中编写FXML文件。 This is true for most GUI screen setups where the overall layout (MenuBar, ToolBar, content, sidebar, ...) is well known and does not change over time. 大多数GUI屏幕设置都是如此,其中整体布局(MenuBar,ToolBar,内容,侧边栏,...)是众所周知的,并且不会随着时间而改变。

Now, custom components do not know how to display itself. 现在,自定义组件不知道如何显示自己。 There are many options to correspond for and most custom components are build up dynamically or can change drastically as the user changes these options. 有许多选项可供选择,大多数自定义组件都是动态构建的,或者在用户更改这些选项时可能会发生巨大变化。 You can not write a single FXML file but instead would have to write many single files, each would build a small part of the custom component. 您不能编写单个FXML文件,而是必须编写许多单个文件,每个文件都会构建自定义组件的一小部分。 This is not really handy to manage, so most developers choose to do it without FXML, directly in code. 这对管理来说并不是很方便,因此大多数开发人员选择直接在代码中使用FXML来完成。

And on top of that: There are many custom components which use a special layout which is not built in into JavaFX, to achieve that you have to write your own containers and wire them up with custom components to get the perfect result. 最重要的是:有许多自定义组件使用特殊的布局而不是内置于JavaFX中,为了实现这一点,您必须编写自己的容器并使用自定义组件连接它们以获得完美的结果。 In the end, custom components are built (or better: should be built) to be used inside FXML / SceneBuilder. 最后,构建自定义组件(或者更好:应该构建)以在FXML / SceneBuilder中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM