使用JavaFx同时处理多个事件

时间:2017-03-03 13:30:48

标签: java javafx

我是javaFx的新手,我想创建一个类似Griddles的游戏。 我想做的第一件事就是了解事件处理的工作原理。

因此,我创建了一个简单的矩形板,并使用MouseEvent连接每个矩形,并创建了一个事件处理程序。

我想做的是: 如果事件是鼠标左键单击,绘制矩形, 否则,如果事件是MouseEnter并且已经按下了鼠标左键,则将当前矩形绘制为红色。

我认为它应该是非常基本的,但是当我实现这个时,我只按了鼠标并输入第一个矩形我点击鼠标,当我解除鼠标左键点击时鼠标进入最后一个矩形..

这是事件处理程序类:

ImageView imView = new ImageView();

Duration timeBetweenImages = Duration.millis(10000);
Timeline timeline = new Timeline();

for (int i = 0; i < imageList.size(); i++) {
    Image image = imageList.get(i);
    Duration frameTime = timeBetweenImages.multiply(i);
    KeyFrame frame = new KeyFrame(frameTime, e -> imView.setImage(image));
    timeline.getKeyFrames().add(frame);
}

timeline.play();

这是我附上活动的地方:

    public void handle(MouseEvent event) {
    if(event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
        System.out.println("Pressed on " + row + ", " + col);
    } else if (event.getEventType().equals(MouseEvent.MOUSE_ENTERED)) {
        System.out.println("Entered " + row + ", " + col);
    }
}

所以,如果我按下0,0中的一个矩形并将鼠标拖动到矩形0,3(同时越过0,1和0的矩形),我得到这个输出:

输入0,0

按0,0

输入0,3

2 个答案:

答案 0 :(得分:1)

如果检测到拖动手势,则鼠标事件仅传递到手势源。没有其他节点收到鼠标事件。

如果可以通过启动完整拖动并侦听MOUSE_DRAG_ENTERED事件来处理此行为:

@Override
public void start(Stage primaryStage) {
    Rectangle rect = new Rectangle(100, 100);
    Rectangle rect2 = new Rectangle(200, 200, 100, 100);

    rect.setOnDragDetected(evt -> {
        // start full drag
        rect.startFullDrag();
    });
    rect2.setOnDragDetected(evt -> {
        // start full drag
        rect2.startFullDrag();
    });

    // print something when mouse enters the rects during a drag event.
    rect.setOnMouseDragEntered(evt -> System.out.println("enter"));
    rect2.setOnMouseDragEntered(evt -> System.out.println("enter"));

    Pane root = new Pane(rect, rect2);
    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}

答案 1 :(得分:0)

这是http://www.java2s.com/Code/Java/JavaFX/Listentoallmouseevents.htm

的控制器版本
@Override
public void initialize(URL url, ResourceBundle rb)
{
     //Handles mouse events
     EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { 
         @Override
         public void handle(MouseEvent mouseEvent) {
             System.out.println(mouseEvent.getEventType() + "\n"
                    + "X : Y - " + mouseEvent.getX() + " : " + mouseEvent.getY() + "\n"
                    + "SceneX : SceneY - " + mouseEvent.getSceneX() + " : " + mouseEvent.getSceneY() + "\n"
                    + "ScreenX : ScreenY - " + mouseEvent.getScreenX() + " : " + mouseEvent.getScreenY());
         }
     };

   //Creates 10 rectangles and set the Mouse events.
   List<Rectangle> rContainer = new ArrayList();
   for(int i = 0; i < 10; i++)
   {
       Rectangle rectangle = new Rectangle();
       rectangle.setX(50);
       rectangle.setY(50);
       rectangle.setWidth(200);
       rectangle.setHeight(100);
       rectangle.setOnMouseClicked(mouseHandler);
       rectangle.setOnMouseDragEntered(mouseHandler);
       rectangle.setOnMouseEntered(mouseHandler);
       rectangle.setFill(Color.BLUE);
       rContainer.add(rectangle);
   }

   //You code will be different here. Here you need to add the arraylist to your root. My root is an AnchorPane with id apMain in FXML
   apMain.getChildren().addAll(rContainer);
}