java JavaFX 2.2 从头开始​​控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12018223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:18:20  来源:igfitidea点击:

JavaFX 2.2 control from scratch

javauser-interfacejavafxjavafx-2

提问by Dmitry

Does anyone know why there are so much examples in Internet about creating your own custom control in JavaFX using SkinBase and BehaviorBase classes from com.sun? Why do they used? Why in FXEnsemble there is no such implementation? Are there any Best Practices for this? Can you give, please, some explanations why I should use BehaviorBase and SkinBase for custom control? Thanks in advance.

有谁知道为什么 Internet 上有这么多关于使用 com.sun 的 SkinBase 和 BehaviorBase 类在 JavaFX 中创建自己的自定义控件的示例?他们为什么使用?为什么在 FXEnsemble 中没有这样的实现?是否有任何最佳实践?请您解释一下为什么我应该使用 BehaviorBase 和 SkinBase 进行自定义控件?提前致谢。

UPDATE:

更新:

I came across this video - http://www.parleys.com/#st=5&id=2789&sl=1, which refers to the creation of custom controls. Author says about Public(Control & Skin) and Private API, and in fact Behavior is a Private API. Is it appropriate to use BehaviorBase and SkinBase from com.sun, which are not described in the official documentation?

我遇到了这个视频 - http://www.parleys.com/#st=5&id=2789&sl=1,它指的是自定义控件的创建。作者说的是Public(Control & Skin)和Private API,其实Behavior是一个Private API。使用com.sun的BehaviorBase和SkinBase是否合适,官方文档中没有描述?

UPDATE 2:

更新 2:

SkinBehaviour and JavaFX 8: http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-February/005874.html

SkinBehaviour 和 JavaFX 8:http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-February/005874.html

回答by jewelsea

Original Answer

原答案

There is a critical feature request to improve support for building custom controlstargeted for delivery for the next major JavaFX release (JavaFX 8.0/JDK 8.0).

有一项关键功能要求改进对构建自定义控件的支持,这些控件旨在为下一个主要 JavaFX 版本 (JavaFX 8.0/JDK 8.0) 交付。

Until the BehaviourBase and SkinBase APIs are made public and other work required to build your own custom controls (such as definition of custom css attributes and pseudo classes) is exposed as public API, the best advice regarding use of these classes is by Richard Bair, the JavaFX platformlead:

在 BehaviourBase 和 SkinBase API 公开并且构建您自己的自定义控件所需的其他工作(例如自定义 css 属性和伪类的定义)公开为公共 API 之前,关于使用这些类的最佳建议是 Richard Bair,在JavaFX平台领先:

If your intent is to write a new control for submission to openjfx . . . then definitely use these classes. If you are writing an application that is meant to work with future versions of the platform and you don't want to break, then don't use these classes.

如果您的目的是编写一个新控件以提交给 openjfx 。. . 那么一定要使用这些类。如果您正在编写一个旨在与平台的未来版本一起使用的应用程序,并且您不想破坏,那么请不要使用这些类。

In any event, use of the BehaviourBase and SkinBase APIs is probably best targeted for controls being built by controls library creators (such as those building for the open-jfxproject for inclusion into the core JavaFX api, or the jfxtrasproject linked in a previous answer). Many re-usable custom application components don't require the complexity introduced by using these classes.

在任何情况下,BehaviourBase 和 SkinBase API 的使用可能最适合由控件库创建者构建的控件(例如那些为open-jfx项目构建以包含在核心 JavaFX api 中的控件,或者在之前的链接中链接的jfxtras项目)。回答)。许多可重用的自定义应用程序组件不需要使用这些类引入的复杂性。

Unless you are creating a generic platform control for a library, you don't need to subclass Controlare likely better off taking one of the following routes:

除非您正在为库创建通用平台控件,否则您不需要子类Control可能最好采用以下路线之一:

  1. Use an existing control and customize it via CSS events and it's public API.
  2. Subclass an existing control to perform more radical changes.
  3. Create your own class which subclasses a container such as a Groupor a layout Panesubclass. In your subclass handle the layout and encapsulate the behaviour of multiple JavaFX nodes and controls.
  1. 使用现有控件并通过 CSS 事件及其公共 API 对其进行自定义。
  2. 子类化现有控件以执行更彻底的更改。
  3. 创建您自己的类,该类将容器(例如Group或布局窗格子类)子类化。在您的子类中处理布局并封装多个 JavaFX 节点和控件的行为。

For examples of creating a custom, reusable component from a Container, see the custom component sectionof the Introduction to FXML document. For an example of subclassing an existing control see the DigitalClock class in this example of a JavaFX clock.

有关从 Container 创建自定义、可重用组件的示例,请参阅FXML 简介文档的自定义组件部分。有关对现有控件进行子类化的示例,请参阅此JavaFX 时钟示例中的 DigitalClock 类。

Update

更新

After I wrote this answer, I reviewed the control creation presentationlinked from the updated answer and it makes the same recommendation as this answer. I'd recommend the presentation to anybody interested in the topic of JavaFX control creation.

在我写下这个答案之后,我查看了从更新的答案链接的控件创建演示,它提出了与这个答案相同的建议。我会向任何对 JavaFX 控件创建主题感兴趣的人推荐该演示文稿。

Update Dec 2014

2014 年 12 月更新

Much of the work has been done in Java 8 to create public APIs for creating controls using JavaFX framework classes. So it possible to build your own custom controls which extend Controlor a Control subclass and make use of SkinBase. Such controls are then similar in structure and API to the built-in JavaFX controls such as Label, Button, etc. Some of information on how to do this is documented in an informal way on the open-jfx wiki, though Oracle has not yet provided complete official documentation on creating such controls on the main JavaFX documentation page.

在 Java 8 中已经完成了大部分工作来创建公共 API,以使用 JavaFX 框架类创建控件。因此可以构建您自己的自定义控件,这些控件扩展Control或 Control 子类并使用SkinBase。这样的控件在结构和 API 上与内置的 JavaFX 控件(如标签、按钮等)相似。有关如何执行此操作的一些信息以非正式方式记录在 open-jfx wiki 上,尽管 Oracle 还没有在主 JavaFX 文档页面上提供了有关创建此类控件的完整官方文档。

One part of the API which was not finalized as a public API is the BehaviourBase class generally used for handling some cross platform specific implementations for tasks such as accelerator key commands for controls, however you can still use the base JavaFX key handling events or third party libraries such as ControlsFX Actionto implement such behavior so that your code does not need to use private API which might change in a future Java release. I believe the BehaviourBase functionality is currently scheduled to be moved to public API classes in the Java 9 timeframe.

API 的一部分未最终确定为公共 API 是 BehaviourBase 类,通常用于处理一些跨平台特定的任务实现,例如控件的加速键命令,但是您仍然可以使用基本的 JavaFX 键处理事件或第三方ControlsFX Action 之类的库来实现这种行为,这样您的代码就不需要使用私有 API,这在未来的 Java 版本中可能会发生变化。我相信 BehaviourBase 功能目前计划在 Java 9 时间范围内转移到公共 API 类。

As stated in the original answer, unless you are creating a generic platform control for a library, you don't need to subclass Control (this is true regardless of the new control creation facilities in JavaFX 8 or 9).

如原始答案中所述,除非您为库创建通用平台控件,否则您不需要子类 Control(无论 JavaFX 8 或 9 中的新控件创建工具如何,都是如此)。

Update Mar 2016

2016 年 3 月更新

For Java 9, a Java Enhancement Proposal (JEP) and related Java bug tracker issue has been created:

对于 Java 9,已创建 Java 增强提案 (JEP) 和相关的 Java 错误跟踪器问题:

The purpose of this effort is to:

这项工作的目的是:

Define public APIs for the JavaFX UI controls and CSS functionality that is presently only available via internal APIs and will hence become inaccessible due to modularization.

为 JavaFX UI 控件和 CSS 功能定义公共 API,这些 API 目前只能通过内部 API 使用,因此由于模块化而无法访问。

Further information on the API changes for custom control support is provided in the linked JEP and bug tracker documentation.

链接的 JEP 和错误跟踪器文档中提供了有关自定义控件支持的 API 更改的更多信息。

An early access release for Java 9 which includes the new API changes is available for downloadand testing.

包含新 API 更改的 Java 9 早期访问版本可供下载和测试。

Note that a public Behavior API is not part of Java 9 and is currently scheduled for implementation in Java 10:

请注意,公共行为 API 不是 Java 9 的一部分,目前计划在 Java 10 中实现:

Other resources

其他资源

If you would like a deep resource on custom JavaFX controls, you might wish to review Hendrik Ebbers book on the subject (I have not read this book):

如果您想要有关自定义 JavaFX 控件的深入资源,您可能希望查看有关该主题的 Hendrik Ebbers 书(我还没有读过这本书):

回答by hansolo_

You might want to take a look at the JFXtras project (http://jfxtras.org). There you will find a lot of custom JavaFX controls that will use SkinBase and BehaviorBase. The source code of the controls could be found on github at https://github.com/JFXtras

您可能想查看 JFXtras 项目 ( http://jfxtras.org)。在那里您会发现许多将使用 SkinBase 和 BehaviorBase 的自定义 JavaFX 控件。控件的源代码可以在https://github.com/JFXtras 的github 上找到