2017-08-29 73 views

回答

0

不指定你想要什麼,但我希望這個例子可以幫助你

第一個例子(我添加相同的事件的所有節點)

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package stackover; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Label; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.GridPane; 

/** 
* 
* @author Ala_Eddine 
*/ 
public class FXMLDocumentController implements Initializable { 

    @FXML 
    private Label label; 
    @FXML 
    private GridPane gridPane; 

    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("You clicked me!"); 
     label.setText("Hello World!"); 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 

     addGridEvent(); 

    } 

    private void addGridEvent() { 
     gridPane.getChildren().forEach(item -> { 
      item.setOnMouseClicked(new EventHandler<MouseEvent>() { 
       @Override 
       public void handle(MouseEvent event) { 
        if (event.getClickCount() == 2) { 
         System.out.println("doubleClick"); 
        } 
        if (event.isPrimaryButtonDown()) { 
         System.out.println("PrimaryKey event"); 
        } 

       } 
      }); 

     }); 
    } 

**} 

第二個例子* *

private void addGridEvent() { 

     Button button = new Button("hi"); 
     //You can use OnAction 
     button.addEventHandler(EventType.ROOT, (event) -> { 
      if (event.getEventType() == ActionEvent.ACTION) { 
       System.out.println("ActionEvent"); 
      } 
     }); 
     gridPane.getChildren().add(button);