JavaFX事件过滤器
2018-01-09 19:24 更新
JavaFX教程 - JavaFX事件过滤器
事件过滤器允许我们在事件捕获阶段处理事件。
节点可以具有用于处理事件的一个或多个过滤器。
我们可以为多个节点使用一个过滤器来处理多个事件类型。
来自父节点的事件过滤器负责处理其子节点,并可拦截事件并防止子节点对事件执行操作。
注册事件过滤器
事件过滤器是EventHandler接口的实现。要注册过滤器,请使用addEventFilter()方法。
// Register an event filter for a single node and a specific event type scene.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { public void handle(MouseEvent e) { System.out.println("mouse clicked"); }; });
完整的源代码
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; //from w ww . ja va 2 s . c o m public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); TextField textBox = new TextField(); textBox.setPromptText("Write here"); // Register an event filter for a single node and a specific event type scene.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { public void handle(MouseEvent e) { System.out.println("mouse clicked"); }; }); root.getChildren().add(textBox); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代码生成以下结果。
过滤器对象
以下代码定义了过滤器对象,并将其用于不同的事件过滤器。
// Define an event filter EventHandler filter = new EventHandler<InputEvent>() { public void handle(InputEvent event) { System.out.println("Filtering out event " + event.getEventType()); event.consume(); } }; // Register the same filter for two different nodes scene.addEventFilter(MouseEvent.MOUSE_PRESSED, filter); // Register the filter for another event type scene.addEventFilter(KeyEvent.KEY_PRESSED, filter);
完整的源代码
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.input.InputEvent; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; /* ww w .ja v a 2s . c o m*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); TextField textBox = new TextField(); textBox.setPromptText("Write here"); // Define an event filter EventHandler filter = new EventHandler<InputEvent>() { public void handle(InputEvent event) { System.out.println("Filtering out event " + event.getEventType()); event.consume(); } }; // Register the same filter for two different nodes scene.addEventFilter(MouseEvent.MOUSE_PRESSED, filter); // Register the filter for another event type scene.addEventFilter(KeyEvent.KEY_PRESSED, filter); root.getChildren().add(textBox); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代码生成以下结果。
删除事件过滤器
要删除事件过滤器,请使用removeEventFilter()方法。此方法将事件类型和过滤器作为参数。
以下代码删除MouseEvent.MOUSEPRESSED事件的过滤器。
// Remove an event filter myNode1.removeEventFilter(MouseEvent.MOUSE_PRESSED, filter);
完整的源代码
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.input.InputEvent; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; // ww w. ja v a 2 s.co m public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); TextField textBox = new TextField(); textBox.setPromptText("Write here"); // Define an event filter EventHandler filter = new EventHandler<InputEvent>() { public void handle(InputEvent event) { System.out.println("Filtering out event " + event.getEventType()); event.consume(); } }; // Register the same filter for two different nodes scene.addEventFilter(MouseEvent.MOUSE_PRESSED, filter); // Register the filter for another event type scene.addEventFilter(KeyEvent.KEY_PRESSED, filter); scene.removeEventFilter(MouseEvent.MOUSE_PRESSED, filter); root.getChildren().add(textBox); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代码生成以下结果。
以上内容是否对您有帮助:
更多建议: