Javafx 简明教程
JavaFX - Event Filters
事件过滤器能够你在事件处理的事件捕获阶段处理事件。事件处理中的事件捕获阶段,是指事件遍历分派链中所有节点的一个阶段。此分派链中的一个节点既可以有一个或多个过滤器,又可以完全没有过滤器,来处理一个事件。
事件过滤器在事件捕获阶段处理事件,例如鼠标事件、滚动事件、键盘事件等。但是,这些事件过滤器需要在节点中注册,才能向节点上生成的事件提供事件处理逻辑。
如果一个节点没有包含事件过滤器,则事件会直接传递到目标节点。否则,一个单一的过滤器可以用于多个节点和事件类型。简单总结来说,事件过滤器用于允许父节点为其子节点提供通用处理;以及截取事件并阻止子节点对事件做出反应。
Adding and Removing Event Filter
要向节点添加事件过滤器,你需要使用 Node 类的 addEventFilter() 方法注册此过滤器。
//Creating the mouse event handler
EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
System.out.println("Hello World");
circle.setFill(Color.DARKSLATEBLUE);
}
};
//Adding event Filter
Circle.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
通过如下所示的方式,你可以用 removeEventFilter() 方法删除过滤器:
circle.removeEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
Example
以下是演示了如何在 JavaFX 中使用事件筛选器处理事件的一个示例。将此代码保存在名为 EventFiltersExample.java 的文件中。
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class EventFiltersExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Circle
Circle circle = new Circle();
//Setting the position of the circle
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
//Setting the radius of the circle
circle.setRadius(25.0f);
//Setting the color of the circle
circle.setFill(Color.BROWN);
//Setting the stroke width of the circle
circle.setStrokeWidth(20);
//Setting the text
Text text = new Text("Click on the circle to change its color");
//Setting the font of the text
text.setFont(Font.font(null, FontWeight.BOLD, 15));
//Setting the color of the text
text.setFill(Color.CRIMSON);
//setting the position of the text
text.setX(150);
text.setY(50);
//Creating the mouse event handler
EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
System.out.println("Hello World");
circle.setFill(Color.DARKSLATEBLUE);
}
};
//Registering the event filter
circle.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
//Creating a Group object
Group root = new Group(circle, text);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting the fill color to the scene
scene.setFill(Color.LAVENDER);
//Setting title to the Stage
stage.setTitle("Event Filters Example");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
javac --module-path %PATH_TO_FX% --add-modules javafx.controls EventFiltersExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls EventFiltersExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。
Example
我们已经看到了如何使用事件筛选器处理鼠标事件。现在,让我们尝试将事件筛选器注册到键盘事件等其他事件中。将此代码保存在名为 EventFilterKeyboard.java 的文件中。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class EventFilterKeyboard extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
//Adding Labels and TextFileds to the scene
Label label1 = new Label("Insert Key");
Label label2 = new Label("Event");
label1.setTranslateX(100);
label1.setTranslateY(100);
label2.setTranslateX(100);
label2.setTranslateY(150);
TextField text1 = new TextField();
TextField text2 = new TextField();
text1.setTranslateX(250);
text1.setTranslateY(100);
text2.setTranslateX(250);
text2.setTranslateY(150);
//Creating EventHandler Object
EventHandler<KeyEvent> filter = new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
text2.setText("Event : "+event.getEventType());
text1.setText(event.getText());
event.consume();
}
};
//Registering Event Filter for the event generated on text field
text1.addEventFilter(KeyEvent.ANY, filter);
//Setting Group and Scene
Group root = new Group();
root.getChildren().addAll(label1,label2,text1,text2);
Scene scene = new Scene(root, 500, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("Adding Event Filter");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
javac --module-path %PATH_TO_FX% --add-modules javafx.controls EventFilterKeyboard.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls EventFilterKeyboard
执行后,上述程序会生成如下所示的 JavaFX 窗口。