JavaFX:EventHandler和EventFilter有什么区别? [英] JavaFX: what is the difference between EventHandler and EventFilter?

查看:361
本文介绍了JavaFX:EventHandler和EventFilter有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一段时间,发现基本上从某些网页来看并没有太大的区别.除了某些要点:

I've been googling for a while and I found that basicly, some web pages says there are no big differences. Except for some points:

  1. EventFilterEventHandler
  2. 之前执行
  3. EventFilterevent.Consume();不敏感 让我看看我是否理解: 可以说我有:

  1. EventFilter is executed before EventHandler
  2. EventFilter is not sensible to event.Consume(); Let me see If I've understood it: Lets say I have:

Button b= new Button("Test"); b.addEventHandler(.....){...}; b.addEventFilter(......){...};

Button b= new Button("Test"); b.addEventHandler(.....){...}; b.addEventFilter(......){...};

假设它们都链接"到MouseEvent.MOUSE_CLICKED;那么,EventFilter的代码将是第一个被执行!!

Let's say they are both "linked" to an MouseEvent.MOUSE_CLICKED; then, EventFilter's code will be the first to be executed!?

比方说,现在,我有:

Button b= new Button("Test");
b.addEventHandler(.....);
b.addEventFilter(......){
  //some code
  event.consume();
}; // First filter
b.addEventFilter(......){
  //some other code
  event.consume();
}; // Second filter

在这种情况下,将执行boath EventFilter,但不会执行.对吧?

In this case, boath EventFilters will be executed but the EventHandler will not. Right?

还有其他需要知道的事情吗?在某些情况下,我应该优先选择一个或另一个?有时我应该一起使用它们来解决一些问题吗?

Are there any other things to know? Are there situations where I should prefere one or other? should I sometimes use them together in order to solve some problems?

谢谢!

推荐答案

每当事件发生时,它都会遵循一个过程来确定场景图中的哪个节点应处理该事件.该过程采取以下步骤:

Whenever an event happens, it follows a process to determine which node in the scene graph should handle the event. The process takes these steps:

  • 目标选择
  • 路线建设
  • 事件捕获<-此处触发了过滤器
  • 事件冒泡<-处理程序在这里触发

目标选择,假设您的场景包含一个带圆圈的窗​​格.如果单击圆圈,则该圆圈将成为事件目标.

Target Selection Say that your scene contains a pane with a circle. If you click on the circle, the circle becomes the event target.

路线构建接下来,JavaFX创建一条路线(或事件分配链).在我们的示例中,链看起来像stage -> scene -> pane -> circle

Route Construction Next, JavaFX creates a route (or an event dispatch chain). In our example the chain would look like stage -> scene -> pane -> circle

事件捕获该事件通过链上的每个事件过滤器传递.只要其中一个过滤器调用consume(),链就会停止,该节点将成为目标.如果没有过滤器调用consume(),则链的末端(圆圈)仍然是目标.

Event Capturing The event gets carried through every event filter on the chain. As soon as one of the filters calls consume(), the chain stops and that node becomes the target. If no filter calls consume() the end of the chain (the circle) remains the target.

事件冒泡接下来,事件再次通过链进行推送,但这一次是从事件目标到阶段.因此,如果窗格事件过滤器名为consume(),则会命中以下事件处理程序:pane -> scene -> stage

Event Bubbling Next, the event get pushed through the chain again, but this time from the event target to the stage. So if the pane event filter called consume(), the following event handlers will be hit: pane -> scene -> stage

因此,不同之处不仅在于这些处理程序被激活时,还在于事件过滤器可以阻止子节点接收事件.

So the difference is not only when these handlers get activated, but also that event filters can prevent child nodes from receiving events.

这篇关于JavaFX:EventHandler和EventFilter有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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