2017-03-03 133 views
0

我是javaFx的新手,我想創建一個類似griddles的遊戲。 我想要做的第一件事是瞭解事件處理如何工作。用JavaFx同時處理多個事件

所以,我創建了一個簡單的矩形板,並用MouseEvent和一個事件處理程序iv'e創建了每個矩形。

我想要做的是: 如果該事件被點擊鼠標左鍵,繪製在矩形, 否則,如果該事件的MouseEnter和鼠標左鍵已經按下,油漆當前矩形紅色。

我想這應該是非常基本的,但是當我實現了這個我只得到鼠標按下並在第一個矩形進我點擊鼠標上的最後一個矩形輸入時,我辭退鼠標左鍵點擊..

這是事件處理類:

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); 
    } 
} 

,這是我附上事件:

private void addRectangles() { 
    for (int row = 0; row < rows; row++) { 
     for (int col = 0; col < cols; col++) { 
      Rectangle rectangle = new Rectangle(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE); 
      rectangle.setStroke(Color.WHITE); 
      rectangle.addEventHandler(MouseEvent.ANY, new ShapeColorChanger(row, col, rectangle)); 
      getChildren().add(rectangle); 
     } 
    } 
} 

所以,如果我在0,0和DRA按下一個矩形gging鼠標矩形0.3(而在0,1和0,2去在矩形)我得到這樣的輸出:

輸入0,0

壓在0,0

輸入0 ,3

+0

控制器版本,你是什麼意思?我也嘗試拆分成MouseEvent.Pressed和MouseEvent.Entered,但得到了相同的結果 – TalOhana

+0

去這裏這是你需要的。 http://www.java2s.com/Code/Java/JavaFX/Listentoallmouseevents.htm – Sedrick

回答

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(); 
} 
+0

它很好用!但只是爲了確保,設置事件是這樣完成的,而不是通過setEventHandler附加事件處理程序?如果有的話,這是更好的做法 – TalOhana

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); 
}