如何在javafx的gridpane中获取列索引和行索引
问题描述:
如何在javafx的gridpane中获取列索引和行索引。请参阅下面的代码
how get column index and row index in gridpane of javafx. see the code below
Text text1 = new Text("Text 1");
Text text2 = new Text("Text 2");
StackPane root = new StackPane();
GridPane gridPane = new GridPane();
gridPane.add(text1, 0, 0);
gridPane.add(text2, 1, 0);
鼠标输入在text1上我想获取GridPane的列索引和行索引
When Mouse Entered On text1 i want to get the column index and row index of GridPane
text1.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
//want to get column index =0 and row index=0
}
});
请告诉我。
答
您可以使用位于GridPane类中的静态方法getRowIndex()和getColumnIndex()来获取行索引和列索引。
You can get the row index and column index by utilising the static methods getRowIndex() and getColumnIndex() which are located in the GridPane class.
System.out.println("Row: "+ GridPane.getRowIndex(text1));
System.out.println("Column: "+ GridPane.getColumnIndex(text1));