Java FXML GridPane.getRowIndex(源代码)在场景生成器中处理鼠标事件时始终返回null

Java FXML GridPane.getRowIndex(源代码)在场景生成器中处理鼠标事件时始终返回null,java,javafx,fxml,scenebuilder,gridpane,Java,Javafx,Fxml,Scenebuilder,Gridpane,我不熟悉javafxml。我还在学习如何使用Scene Builder 2.0和控制器类。我不明白为什么返回空值 @FXML public void gridClick(MouseEvent e){ Node source = (Node)e.getSource() ; Integer colIndex = grid.getColumnIndex(source); Integer rowIndex = grid.getRowIndex(source); Syste

我不熟悉javafxml。我还在学习如何使用Scene Builder 2.0和控制器类。我不明白为什么返回空值

@FXML
public void gridClick(MouseEvent e){
    Node source = (Node)e.getSource() ;
    Integer colIndex = grid.getColumnIndex(source);
    Integer rowIndex = grid.getRowIndex(source);
    System.out.println("Mouse entered cell ["+colIndex+","+rowIndex+"]");
}
我启动了SceneBuilder中的所有网格单元 然后,我为控制器类初始化中的每个单元创建了锚烷:

for(int row=0;row<numRows;row++){
    for(int col=0;col<numCols;col++){
        AnchorPane slot=new AnchorPane();
        slot.setStyle("-fx-border-color: black;-fx-border-width: 0.2;");
        grid.add(slot,col,row);
    }
}

用于(int row=0;rowI将首先验证
source
实际上是AnchorPane。我还将验证它的父项是GridPane。这里,“grid”是GridPane。因此我认为它将添加为“grid”的子项。我如何检查源的类型?我已经检查了AnchorPane标识符的父项和
source
,并且它是
grid
。它仍然返回null。这是一种不好的方法@tammybamy,您应该使用
控制器
,在JavaFX中它更好更容易“寻求调试帮助的问题”(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误,以及在问题本身中重现问题所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅: