Javafx 简明教程

JavaFX - Event Handling

在 JavaFX 中,我们可以开发 GUI 应用程序、Web 应用程序和图形应用程序。在这样的应用程序中,用户与应用程序(节点)交互时,就会说有一个事件已经发生。

In JavaFX, we can develop GUI applications, web applications and graphical applications. In such applications, whenever a user interacts with the application (nodes), an event is said to have been occurred.

例如,单击按钮、移动鼠标、通过键盘输入字符、从列表中选择项目、滚动页面都是导致事件发生的活动。

For example, clicking on a button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page are the activities that causes an event to happen.

Types of Events

事件可以大致分为以下两类 −

The events can be broadly classified into the following two categories −

  1. Foreground Events − Those events which require the direct interaction of a user. They are generated as consequences of a person interacting with the graphical components in a Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page, etc.

  2. Background Events − Those events that don’t require the interaction of end-user are known as background events. The operating system interruptions, hardware or software failure, timer expiry, operation completion are the example of background events.

Events in JavaFX

JavaFX 提供支持以处理各种事件。名为 Event 的包中的类是事件的基本类。

JavaFX provides support to handle a wide varieties of events. The class named Event of the package javafx.event is the base class for an event.

任何其子类的实例都是一个事件。JavaFX 提供了各种各样的事件。其中一些如下所示。

An instance of any of its subclass is an event. JavaFX provides a wide variety of events. Some of them are are listed below.

  1. Mouse Event − This is an input event that occurs when a mouse is clicked. It is represented by the class named MouseEvent. It includes actions like mouse clicked, mouse pressed, mouse released, mouse moved, mouse entered target, mouse exited target, etc.

  2. Key Event − This is an input event that indicates the key stroke occurred on a node. It is represented by the class named KeyEvent. This event includes actions like key pressed, key released and key typed.

  3. Drag Event − This is an input event which occurs when the mouse is dragged. It is represented by the class named DragEvent. It includes actions like drag entered, drag dropped, drag entered target, drag exited target, drag over, etc.

  4. Window Event − This is an event related to window showing/hiding actions. It is represented by the class named WindowEvent. It includes actions like window hiding, window shown, window hidden, window showing, etc.

Event Handling

事件处理是控制事件的机制,并决定如果事件发生,该发生什么。此机制具有称为事件处理程序的代码,当事件发生时执行该代码。

Event Handling is the mechanism that controls the event and decides what should happen, if an event occurs. This mechanism has the code which is known as an event handler that is executed when an event occurs.

JavaFX 提供处理程序和过滤器来处理事件。在 JavaFX 中,每个事件都有 −

JavaFX provides handlers and filters to handle events. In JavaFX every event has −

  1. Target − The node on which an event occurred. A target can be a window, scene, and a node.

  2. Source − The source from which the event is generated will be the source of the event. In the above scenario, mouse is the source of the event.

  3. Type − Type of the occurred event; in case of mouse event – mouse pressed, mouse released are the type of events.

假设我们有一个应用程序,其中插入了一个圆形、停止和播放按钮,如下所示,使用了一个组对象 −

Assume that we have an application which has a Circle, Stop and Play Buttons inserted using a group object as follows −

sample application

如果单击播放按钮,源将是鼠标,目标节点将是播放按钮,并且生成的事件的类型是鼠标点击。

If you click on the play button, the source will be the mouse, the target node will be the play button and the type of the event generated is the mouse click.

Phases of Event Handling in JavaFX

每当产生事件时,JavaFX 会经历以下阶段。

Whenever an event is generated, JavaFX undergoes the following phases.

Route Construction

每次生成一个事件时,事件的默认/初始路由由 Event Dispatch chain 的构建来确定。它从舞台到源节点的一条路径。

Whenever an event is generated, the default/initial route of the event is determined by construction of an Event Dispatch chain. It is the path from the stage to the source Node.

在上面的场景中,当我们点击播放按钮时,以下是为所生成的事件分发的事件分发链。

Following is the event dispatch chain for the event generated, when we click on the play button in the above scenario.

play button

Event Capturing Phase

在构建事件分发链之后,应用程序的根节点分发该事件。此事件会传递到分发链中的所有节点(从上到下)。如果其中任何一个节点具有为所生成事件注册的 filter ,它将被执行。如果分发链中的任何节点都没有针对所生成事件的过滤器,则它将被传递到目标节点,最后目标节点将处理该事件。

After the construction of the event dispatch chain, the root node of the application dispatches the event. This event travels to all nodes in the dispatch chain (from top to bottom). If any of these nodes has a filter registered for the generated event, it will be executed. If none of the nodes in the dispatch chain has a filter for the event generated, then it is passed to the target node and finally the target node processes the event.

Event Bubbling Phase

在事件冒泡阶段,事件从目标节点到舞台节点(从下到上)传递。如果事件分发链中的任何一个节点具有为所生成事件注册的 handler ,它将被执行。如果所有这些节点都没有处理器来处理事件,则事件将到达根节点,最终进程将完成。

In the event bubbling phase, the event is travelled from the target node to the stage node (bottom to top). If any of the nodes in the event dispatch chain has a handler registered for the generated event, it will be executed. If none of these nodes have handlers to handle the event, then the event reaches the root node and finally the process will be completed.

Event Handlers and Filters

事件过滤器和处理器包含用于处理事件的应用程序逻辑。一个节点可以注册到多个处理器/过滤器。对于父子节点,您可以为父节点提供一个公共过滤器/处理器,此过滤器/处理器作为所有子节点的默认值进行处理。

Event filters and handlers are those which contains application logic to process an event. A node can register to more than one handler/filter. In case of parent–child nodes, you can provide a common filter/handler to the parents, which is processed as default for all the child nodes.

如上文所述,在事件处理期间,过滤器将被执行,在事件冒泡阶段,处理器将被执行。所有处理器和过滤器都实现包 javafx.event 的接口 EventHandler

As mentioned above, during the event, processing is a filter that is executed and during the event bubbling phase, a handler is executed. All the handlers and filters implement the interface EventHandler of the package javafx.event.