我们应该在JavaFX自定义控件中使用FXML吗? [英] Should we use FXML in JavaFX custom controls or not?

查看:583
本文介绍了我们应该在JavaFX自定义控件中使用FXML吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有点问这个,我认为使用FXML来编写我们的自定义组件显然是正确的方法。



但我们可以看到来自ControlsFX,JFXextras甚至是掌握JavaFX8控件一书中没有使用或提及在自定义控件中使用FXML。



尽管如此,官方文档要说去在那条路线上,并通过FXML创建JavaFX控件。



更正确的方法是什么?为什么?

解决方案

JavaFX中有两种自定义控件:




  • fx:root基于自定义控件:这些自定义控件是可定制的(支持CSS),但不可换肤。



    如果您是应用程序开发人员,那么您最想要使用的是



    这种控件通常是应用程序或组织特定的。不需要这些控件的额外自定义皮肤,CSS就足够了。



    通常他们只是将一些控件组合在一起并提供更好的API并隐藏内部。



    它们比skinnable自定义控件更容易编写。



    我认为这是JPanel子类的替代品在Swing-world中。



    我建议将你的GUI分解为小的
    自包含的fx:基于root的控件(高内聚,低耦合)。使用
    Mediator Pattern来组合这些控件(父控件
    管理/配置其子控件并侦听其子控件的
    事件。子控件彼此不了解)。



    您可以创建此类控件的整个层次结构。将
    分解为小型控件将使维护更容易(例如,如果布局或
    用户交互必须更改)。


  • skinnable自定义控件:skinnable自定义控件不仅可以设置样式(通过CSS),而且可以通过提供自定义皮肤来完全更改其外观。



    <如果你是一个控件库开发人员,那么你很可能想要提供可换肤的自定义控件,以允许库的用户最大限度地利用JavaFX。



    这个一种控件通常是非常基本的,高度可重复使用的控件,并且通常(但并不总是)使用一些自定义绘画。



    他们比fx更难做到:root基于自定义控件。



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.

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.

Despite that, the official documentation to say to go on that route, and create JavaFX controls via FXML.

What is the more correct way and why ?

解决方案

There are two kinds of custom controls in JavaFX:

  • fx:root based custom controls: These custom controls are styleable (support CSS), but are not skinnable.

    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.

    Usually they just group some controls together and provide a nicer API and hide the internals.

    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.

    I suggest to break down your GUI into small self-contained fx:root based controls (high cohesion, low coupling). 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).

    You can create whole hierarchies of such controls. Breaking them down into small controls will make maintenance easier (e.g. 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.

    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.

    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.

这篇关于我们应该在JavaFX自定义控件中使用FXML吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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