JavaFX - 事件处理

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

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

事件类型

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

  • 前景事件 : 那些需要用户直接交互的事件.它们是作为人与图形用户界面中的图形组件交互的结果而生成的.例如,单击按钮,移动鼠标,通过键盘输入字符,从列表中选择项目,滚动页面等等.

  • 背景活动 : 那些需要最终用户交互的事件称为后台事件.操作系统中断,硬件或软件故障,计时器到期,操作完成是后台事件的示例.

JavaFX中的事件

JavaFX为处理各种事件提供支持.包 javafx.event 的名为 Event 的类是事件的基类.

任何子类的实例是一个事件. JavaFX提供了各种各样的事件.其中一些列在下面.

  • 鼠标事件 : 这是单击鼠标时发生的输入事件.它由名为 MouseEvent 的类表示.它包括鼠标点击,鼠标按下,鼠标释放,鼠标移动,鼠标输入目标,鼠标退出目标等操作.

  • 关键事件 : 这是一个输入事件,指示节点上发生的键击.它由名为 KeyEvent 的类表示.此事件包括按下键,释放键和键入键等操作.

  • 拖动事件 : 这是拖动鼠标时发生的输入事件.它由名为 DragEvent 的类表示.它包括拖动输入,拖动拖动,拖动输入目标,拖动退出目标,拖动等操作.

  • 窗口事件  : 去;这是与窗口显示/隐藏操作相关的事件.它由名为 WindowEvent 的类表示.它包括窗口隐藏,窗口显示,隐藏窗口,窗口显示等操作.

事件处理

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

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

  • 目标 : 发生事件的节点.目标可以是窗口,场景和节点.

  • 来源 : 生成事件的源将是事件的来源.在上面的场景中,鼠标是事件的来源.

  • 类型 : 发生事件的类型;在鼠标事件的情况下 - 鼠标按下,鼠标被释放是事件的类型.

假设我们有一个有圆圈的应用程序,使用组对象插入停止和播放按钮,如下所示;

示例应用程序

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

JavaFX中事件处理的阶段

每当生成事件时,JavaFX都会经历以下阶段.

路径构建

每当生成事件时,事件的默认/初始路由由构建事件调度链确定.它是从阶段到源节点的路径.

以下是当我们点击上述场景中的播放按钮时生成的事件的事件派发链.

播放按钮

事件捕获阶段

构建事件调度链后,应用程序的根节点将调度该事件.此事件将传递到调度链中的所有节点(从上到下).如果这些节点中的任何节点为生成的事件注册了过滤器,则将执行该节点.如果调度链中的任何节点都没有生成事件的过滤器,则将其传递给目标节点,最后目标节点处理该事件.

事件冒泡阶段

在事件冒泡阶段,事件从目标节点传到阶段节点(从下到上).如果事件调度链中的任何节点具有为生成的事件注册的处理程序,则将执行该节点.如果这些节点都没有处理事件的处理程序,则事件到达根节点,最后进程将完成.

事件处理程序和过滤器

事件过滤器和处理程序是包含处理事件的应用程序逻辑的过滤器和处理程序.节点可以注册到多个处理程序/过滤器.在父子节点的情况下,您可以为父节点提供一个公共过滤器/处理程序,它将作为所有子节点的默认处理.

如上所述,在事件期间, processing是一个执行的过滤器,在事件冒泡阶段,执行一个处理程序.所有处理程序和过滤器都实现了包 javafx.event 的接口 EventHandler .

添加和删除事件过滤器

要向节点添加事件过滤器,您需要使用 Node 类的方法 addEventFilter()注册此过滤器.

 //创建鼠标事件处理程序
 EventHandler< MouseEvent> eventHandler = new EventHandler< MouseEvent>(){
 @Override 
 public void handle(MouseEvent e){
 System.out.println("Hello World"); 
 circle.setFill(Color.DARKSLATEBLUE); 
} 
}; 
//添加事件过滤器
 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 EventFiltersExample.java 
 java EventFiltersExample

执行时,上述程序生成一个JavaFX窗口,如下所示.

更改颜色

添加和删除事件处理程序

要向节点添加事件处理程序,您需要使用 Node 的方法 addEventHandler()注册此处理程序> class如下所示.

//创建鼠标事件处理程序
 EventHandler< javafx.scene.input.MouseEvent> eventHandler = 
 new EventHandler< javafx.scene.input.MouseEvent>(){
 @Override 
 public void handle(javafx.scene.input.MouseEvent e){
 System.out.println("Hello World"); 
 circle.setFill(Color.DARKSLATEBLUE); 
} 
}; 
//添加事件处理程序
 circle.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED,eventHandler);

以同样的方式,您可以使用removeEventHandler()方法删除事件处理程序,如下所示 :

circle.removeEventHandler(MouseEvent.MOUSE_CLICKED,eventHandler);

示例

以下程序是一个使用事件处理程序演示JavaFX中事件处理的示例.

将此代码保存在名为 EventHandlersExample.java 的文件中.

import javafx.animation.RotateTransition; 
import javafx.application.Application; 
import javafx.event.EventHandler; 

import javafx.scene.Group; 
import javafx.scene.PerspectiveCamera; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.paint.Color; 
import javafx.scene.paint.PhongMaterial;
 
import javafx.scene.shape.Box; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text;  
import javafx.scene.transform.Rotate; 
import javafx.stage.Stage; 
import javafx.util.Duration; 
         
public class EventHandlersExample extends Application { 
   
   @Override 
   public void start(Stage stage) {
      //Drawing a Box 
      Box box = new Box(); 
      
      //Setting the properties of the Box 
      box.setWidth(150.0); 
      box.setHeight(150.0);   
      box.setDepth(100.0); 
       
      //Setting the position of the box 
      box.setTranslateX(350);  
      box.setTranslateY(150); 
      box.setTranslateZ(50); 
       
      //Setting the text 
      Text text = new Text("Type any letter to rotate the box, 
         and click on the box to stop the rotation"); 
      
      //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(20); 
      text.setY(50); 
       
      //Setting the material of the box 
      PhongMaterial material = new PhongMaterial();  
      material.setDiffuseColor(Color.DARKSLATEBLUE);  
      
      //Setting the diffuse color material to box 
      box.setMaterial(material);       
       
      //Setting the rotation animation to the box    
      RotateTransition rotateTransition = new RotateTransition(); 
      
      //Setting the duration for the transition 
      rotateTransition.setDuration(Duration.millis(1000)); 
      
      //Setting the node for the transition 
      rotateTransition.setNode(box);       
      
      //Setting the axis of the rotation 
      rotateTransition.setAxis(Rotate.Y_AXIS); 
      
      //Setting the angle of the rotation
      rotateTransition.setByAngle(360); 
      
      //Setting the cycle count for the transition 
      rotateTransition.setCycleCount(50); 
      
      //Setting auto reverse value to false 
      rotateTransition.setAutoReverse(false);  
      
      //Creating a text filed 
      TextField textField = new TextField();   
      
      //Setting the position of the text field 
      textField.setLayoutX(50); 
      textField.setLayoutY(100); 
       
      //Handling the key typed event 
      EventHandler<KeyEvent> eventHandlerTextField = new EventHandler<KeyEvent>() { 
         @Override 
         public void handle(KeyEvent event) { 
            //Playing the animation 
            rotateTransition.play(); 
         }           
      };              
      //Adding an event handler to the text feld 
      textField.addEventHandler(KeyEvent.KEY_TYPED, eventHandlerTextField); 
       
      //Handling the mouse clicked event(on box) 
      EventHandler<javafx.scene.input.MouseEvent> eventHandlerBox = 
         new EventHandler<javafx.scene.input.MouseEvent>() { 
         
         @Override 
         public void handle(javafx.scene.input.MouseEvent e) { 
            rotateTransition.stop();  
         } 
      }; 
      //Adding the event handler to the box  
      box.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, eventHandlerBox);
       
      //Creating a Group object
      Group root = new Group(box, textField, text); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);      
      
      //Setting camera 
      PerspectiveCamera camera = new PerspectiveCamera(false); 
      camera.setTranslateX(0); 
      camera.setTranslateY(0); 
      camera.setTranslateZ(0); 
      scene.setCamera(camera);  
      
      //Setting title to the Stage 
      stage.setTitle("Event Handlers 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 EventHandlersExample.java 
 java EventHandlersExample

执行时,上述程序生成显示文本字段和3D框的JavaFX窗口,如下所示 :

Text Field

在此处,如果在文本字段中键入字母,则3D框将开始沿x轴旋转.如果再次单击该框,则旋转将停止.

使用事件处理的便捷方法

JavaFX中的某些类定义事件处理程序属性.通过使用各自的setter方法将值设置为这些属性,您可以注册到事件处理程序.这些方法称为便捷方法.

这些方法中的大多数存在于Node,Scene,Window等类中,并且它们可用于所有子类.

例如,要向按钮添加鼠标事件监听器,可以使用方便方法 setOnMouseClicked(),如下所示.

playButton.setOnMouseClicked((new EventHandler<MouseEvent>() { 
   public void handle(MouseEvent event) { 
      System.out.println("Hello World"); 
      pathTransition.play(); 
   } 
}));

示例

以下程序是使用便捷方法演示JavaFX中事件处理的示例.

将此代码保存在名为 ConvinienceMethodsExample.java 的文件中.

import javafx.animation.PathTransition; 
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.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.paint.Color; 

import javafx.scene.shape.Circle; 
import javafx.scene.shape.LineTo; 
import javafx.scene.shape.MoveTo; 
import javafx.scene.shape.Path; 
import javafx.stage.Stage; 
import javafx.util.Duration; 
         
public class ConvinienceMethodsExample 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);      
       
      //Creating a Path 
      Path path = new Path(); 
      
      //Moving to the staring point 
      MoveTo moveTo = new MoveTo(208, 71);               
      
      //Creating 1st line 
      LineTo line1 = new LineTo(421, 161);        
      
      //Creating 2nd line 
      LineTo line2 = new LineTo(226,232); 
      
      //Creating 3rd line 
      LineTo line3 = new LineTo(332,52);        
      
      //Creating 4th line 
      LineTo line4 = new LineTo(369, 250);        
      
      //Creating 5th line 
      LineTo line5 = new LineTo(208, 71);       
      
      //Adding all the elements to the path 
      path.getElements().add(moveTo); 
      path.getElements().addAll(line1, line2, line3, line4, line5);     
      
      //Creating the path transition 
      PathTransition pathTransition = new PathTransition(); 
      
      //Setting the duration of the transition 
      pathTransition.setDuration(Duration.millis(1000));       
      
      //Setting the node for the transition 
      pathTransition.setNode(circle); 
      
      //Setting the path for the transition 
      pathTransition.setPath(path); 
      
      //Setting the orientation of the path 
      pathTransition.setOrientation(
         PathTransition.OrientationType.ORTHOGONAL_TO_TAN GENT);
      
      //Setting the cycle count for the transition 
      pathTransition.setCycleCount(50); 
      
      //Setting auto reverse value to true 
      pathTransition.setAutoReverse(false);
      
      //Creating play button 
      Button playButton = new Button("Play"); 
      playButton.setLayoutX(300); 
      playButton.setLayoutY(250); 
       
      circle.setOnMouseClicked (new EventHandler<javafx.scene.input.MouseEvent>() { 
         @Override 
         public void handle(javafx.scene.input.MouseEvent e) { 
            System.out.println("Hello World"); 
            circle.setFill(Color.DARKSLATEBLUE);             
         } 
      });   
      playButton.setOnMouseClicked((new EventHandler<MouseEvent>() { 
         public void handle(MouseEvent event) { 
            System.out.println("Hello World");  
            pathTransition.play(); 
         } 
      })); 
       
      //Creating stop button 
      Button stopButton = new Button("stop"); 
      stopButton.setLayoutX(250); 
      stopButton.setLayoutY(250); 
      
      stopButton.setOnMouseClicked((new EventHandler<MouseEvent>() { 
         public void handle(MouseEvent event) { 
            System.out.println("Hello World"); 
            pathTransition.stop(); 
         } 
      }));
      //Creating a Group object  
      Group root = new Group(circle, playButton, stopButton); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300); 
      scene.setFill(Color.LAVENDER);  
      
      //Setting title to the Stage 
      stage.setTitle("Convenience Methods 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 ConvinienceMethodsExample.java 
 java ConvinienceMethodsExample

执行时,上述程序生成一个JavaFX窗口,如下所示.点击播放按钮开始动画,然后点击停止按钮停止动画.

Convinience Method