Javafx 如何单击GridPane单元格并使其执行操作?

Javafx 如何单击GridPane单元格并使其执行操作?,javafx,java-8,Javafx,Java 8,我不熟悉JavaFX(Java就是这样),我希望能够单击一个GridPane,让它在侧面板中显示我的房间属性(或者在这里显示到控制台)。我已经成功地设置了一个鼠标事件,但我认为它不是适合这项工作的工具。当我单击网格中的任何位置时,它返回“null”,并且不会给我单元格坐标(这里可能有更好或更有用的信息可以收集?) 我也在使用场景生成器 控制器类 import Data.Area; import Model.Grid; import Model.TileSet; import javafx.ev

我不熟悉JavaFX(Java就是这样),我希望能够单击一个GridPane,让它在侧面板中显示我的房间属性(或者在这里显示到控制台)。我已经成功地设置了一个鼠标事件,但我认为它不是适合这项工作的工具。当我单击网格中的任何位置时,它返回“null”,并且不会给我单元格坐标(这里可能有更好或更有用的信息可以收集?)

我也在使用场景生成器

控制器类

import Data.Area;
import Model.Grid;
import Model.TileSet;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import java.io.IOException;
import java.io.InputStream;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Controller {
    InputStream rinput = getClass().getResourceAsStream("/red.png");
    Image red = new Image(rinput);
    ImageView redimg = new ImageView(red);


    InputStream binput = getClass().getResourceAsStream("/black.png");
    Image black = new Image(binput);

    InputStream pinput = getClass().getResourceAsStream("/pink.png");
    Image pink = new Image(binput);

    @FXML
    public GridPane gridmane;


    public void genSmall(ActionEvent actionEvent) throws IOException {

        Grid grid = new Grid(new Area(40, 40));
        grid.getPathfinder().shufflePartitions();
        grid.getPathfinder().fillPartitions();
        grid.getPathfinder().generateHallways();
        grid.getPathfinder().placeWalls();
        importGrid(gridmane, grid);

        gridmane.getScene().getWindow().sizeToScene();
    }

    public void genMed(ActionEvent actionEvent) throws IOException {

        Grid grid = new Grid(new Area(60, 60));
        grid.getPathfinder().shufflePartitions();
        grid.getPathfinder().fillPartitions();
        grid.getPathfinder().generateHallways();
        grid.getPathfinder().placeWalls();
        gridmane.getScene().getWindow().sizeToScene();
        gridmane.getScene().getWindow().setHeight(600);
        gridmane.getScene().getWindow().setWidth(600);
        importGrid(gridmane, grid);


    }

    public void genLarge(ActionEvent actionEvent) throws IOException {

        Grid grid = new Grid(new Area(80, 80));
        grid.getPathfinder().shufflePartitions();
        grid.getPathfinder().fillPartitions();
        grid.getPathfinder().generateHallways();
        grid.getPathfinder().placeWalls();
        gridmane.getScene().getWindow().sizeToScene();
        gridmane.getScene().getWindow().setHeight(800);
        gridmane.getScene().getWindow().setWidth(800);
        importGrid(gridmane, grid);


    }

    private void importGrid(GridPane gridPane, Grid grid) {
        gridPane.getChildren().clear(); // remove old children

        for (int i = 0; i < grid.getSize().height; i++) {
            for (int j = 0; j < grid.getSize().width; j++) {
                if (grid.getContent()[j + (i * grid.getSize().width)] == TileSet.floorTile) {
                    changeSquare(gridPane, i, j, Color.WHITE, red);
                }
                else if (grid.getContent()[j + (i * grid.getSize().width)] == TileSet.wallTile) {

                    changeSquare(gridPane, i, j, Color.GRAY, black);

                }
                else {
                    changeSquare(gridPane, i, j, Color.BLACK, pink);
                }
            }
        }
    }

    private void changeSquare(GridPane gridPane, int xCoordinate, int yCoordinate, Color color, Image image) {
        Rectangle rect = new Rectangle();
        ImageView fimage = new ImageView(image);
        rect.setStroke(Color.BLACK);
        rect.setFill(color);
        rect.setWidth(10);
        rect.setHeight(10);
        gridPane.add(fimage, xCoordinate, yCoordinate);
    }




    public void clickGrid(javafx.scene.input.MouseEvent event) {
        Node source = (Node)event.getSource() ;
        Integer colIndex = gridmane.getColumnIndex(source);
        Integer rowIndex = gridmane.getRowIndex(source);
        System.out.println("Mouse clicked cell: " + colIndex + "And: " + rowIndex);


    }
}
FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>

<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="258.0" prefWidth="332.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
   <children>
      <MenuBar>
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
         <children>
            <Button alignment="CENTER" mnemonicParsing="false" onAction="#genSmall" prefHeight="27.0" prefWidth="64.0" text="Small" HBox.hgrow="ALWAYS">
               <HBox.margin>
                  <Insets left="20.0" />
               </HBox.margin>
            </Button>
            <Button alignment="CENTER" mnemonicParsing="false" onAction="#genMed" text="Medium" HBox.hgrow="ALWAYS">
               <HBox.margin>
                  <Insets left="20.0" />
               </HBox.margin>
            </Button>
            <Button alignment="CENTER" mnemonicParsing="false" onAction="#genLarge" prefHeight="27.0" prefWidth="66.0" text="Large" HBox.hgrow="ALWAYS">
               <HBox.margin>
                  <Insets left="20.0" />
               </HBox.margin>
            </Button>
         </children>
      </HBox>
      <StackPane>
         <children>
             <GridPane fx:id="gridmane" maxHeight="-Infinity" maxWidth="-Infinity" onMouseClicked="#clickGrid" VBox.vgrow="NEVER" />
         </children>
      </StackPane>
   </children>
</VBox>

由于您在
GridPane
上注册了事件处理程序,因此事件的源是
GridPane
本身。您没有为
GridPane
设置行/列索引,并且它不会包含任何有用的信息

在这种情况下,您需要从
MouseEvent
获取实际单击的节点:

public void clickGrid(javafx.scene.input.MouseEvent event) {
    Node clickedNode = event.getPickResult().getIntersectedNode();
    if (clickedNode != gridmane) {
        // click on descendant node
        Integer colIndex = GridPane.getColumnIndex(clickedNode);
        Integer rowIndex = GridPane.getRowIndex(clickedNode);
        System.out.println("Mouse clicked cell: " + colIndex + " And: " + rowIndex);
    }
}
在这种情况下,代码是这样工作的,因为
GridPane
的子节点没有自己的子节点,这些子节点可能是相交节点

如果要添加可能有自己子节点的子节点,则需要遍历节点层次结构,直到找到
网格窗格的子节点为止:

if (clickedNode != gridmane) {
    // click on descendant node
    Node parent = clickedNode.getParent();
    while (parent != gridmane) {
        clickedNode = parent;
        parent = clickedNode.getParent();
    }
    Integer colIndex = GridPane.getColumnIndex(clickedNode);
    Integer rowIndex = GridPane.getRowIndex(clickedNode);
    System.out.println("Mouse clicked cell: " + colIndex + " And: " + rowIndex);
}

你能分享面积、网格和瓷砖的代码吗?
if (clickedNode != gridmane) {
    // click on descendant node
    Node parent = clickedNode.getParent();
    while (parent != gridmane) {
        clickedNode = parent;
        parent = clickedNode.getParent();
    }
    Integer colIndex = GridPane.getColumnIndex(clickedNode);
    Integer rowIndex = GridPane.getRowIndex(clickedNode);
    System.out.println("Mouse clicked cell: " + colIndex + " And: " + rowIndex);
}