JavaFX - 事件過濾器



事件過濾器允許您在事件處理的事件捕獲階段處理事件。事件處理中的事件捕獲階段是指事件在排程鏈中遍歷所有節點的階段。此排程鏈中的節點可以擁有一個或多個過濾器來處理事件,也可以根本沒有過濾器。

事件過濾器在此事件捕獲階段處理事件,例如滑鼠事件、滾動事件、鍵盤事件等。但是,這些事件過濾器需要在節點上註冊,以便為在節點上生成的事件提供事件處理邏輯。

如果節點不包含事件過濾器,則事件將直接傳遞給目標節點。否則,單個過濾器可用於多個節點和事件型別。簡單總結一下,事件過濾器用於使父節點能夠為其子節點提供通用處理;以及攔截事件並阻止子節點對事件進行操作。

新增和刪除事件過濾器

要向節點新增事件過濾器,您需要使用**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);

示例

以下是一個示例,演示了使用事件過濾器在 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 視窗。

Change Color

示例

我們已經看到了事件過濾器如何處理滑鼠事件。現在,讓我們嘗試在其他事件(如鍵盤事件)上註冊事件過濾器。將此程式碼儲存在名為**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 視窗。

Keyboard Event Filters
廣告