How to make JavaFX multi-click mouse events?

JavaFX Production mouse double-click or multi-click events need to use getClickCount () method, which you need to add addEventHandler () method , addEventHandler () event method.

 1 scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
 2 
 3             @Override
 4             public void handle(MouseEvent event) {
 5                 int times=event.getClickCount();
 6                 if(times==2) {
 7                     System.out.println("2次"); 8  } 9  } 10 });
In this code, scene knowledge in order to test use, all controls have this basic method, such as buttons and so on.
We should note that two further arguments are to be filled is something you want to monitor, mouse events is not as good as MouseEvent , followed by specific events MOUSE_CLICKED .
The second parameter is an object, which can implement your approach!
 1 package text1;
 2 
 3 import javafx.application.Application;
 4 import javafx.event.EventHandler;
 5 import javafx.scene.Group;
 6 import javafx.scene.Scene;
 7 import javafx.scene.input.MouseEvent; 8 import javafx.stage.Stage; 9 10 public class Main extends Application { 11 12  @Override 13 public void start(Stage stage) throws Exception { 14 Group root=new Group(); 15 Scene scene=new Scene(root); 16 stage.setWidth(500); 17 scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { 18 19  @Override 20 public void handle(MouseEvent event) { 21 int times=event.getClickCount(); 22 if(times==2) { 23 System.out.println("2次"); 24  } 25  } 26  }); 27  stage.setScene(scene); 28  stage.show(); 29  } 30 31 public static void main(String[] args) { 32  launch(args); 33  } 34 35 }

Guess you like

Origin www.cnblogs.com/modulecode/p/12079392.html