提问者:小点点

在GridPane JavaFx中选择单元格


长话短说,我有8x8网格窗格(用它作为棋盘),我希望能够点击每个单元格并获得其坐标。

public class BoardView {     
    private ImageView imageView = new ImageView(new Image("board.png"));
    private GridPane boardGrid = new GridPane();

    public void createBoard(){
        boardGrid.getChildren().add(imageView);
        for(int i =0;i < 8; i++){
            for(int j = 0; j < 8; j++){
                Tile tile = new Tile(i, j);
                GridPane.setConstraints(tile.getPane(), i, j);
                boardGrid.getChildren().add(tile.getPane());
            }
        }

    }

    class Tile {
        private int positionX;
        private int positionY;
        private Pane pane;

        Tile(int x, int y) {
            pane = new Pane();                
            positionX = x;
            positionY = y;
            pane.setOnMouseClicked(e -> {
                        System.out.println(positionX + " " + positionY);
                    }
            );
        }
    }

然而,无论我点击哪里,结果都是“0 0”,而不是实际的行/列位置。


共1个答案

匿名用户

您的代码不完整,您的一些错误是:

>

  • 您没有在每个窗格(磁贴)上给出特定的大小(宽度、高度)

    我猜你在某个地方设置了网格窗格的大小,但这只是一种猜测,现在你在网格上添加背景图像的方式是我不建议使用StackPane。

    这是一个小例子,您可以检查它来调试您的问题。

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class BoardView extends Application {
    
        // the dimensions of our background Image
        private final int BORDER_WIDTH = 695;
        private final int BORDER_HEIGHT = 720;
    
        @Override
        public void start(Stage stage) throws Exception {
    
            // Load your Image
            ImageView backgroundImageView = new ImageView(
                    new Image("https://cdn.pixabay.com/photo/2013/07/13/10/24/board-157165_960_720.png"));
            // Initialize the grid
            GridPane boardGrid = initBoard();
            // Set the dimensions of the grid
            boardGrid.setPrefSize(BORDER_WIDTH, BORDER_HEIGHT);
    
            // Use a StackPane to display the Image and the Grid
            StackPane mainPane = new StackPane();
            mainPane.getChildren().addAll(backgroundImageView, boardGrid);
    
            stage.setScene(new Scene(mainPane));
            stage.setResizable(false);
            stage.show();
    
        }
    
        private GridPane initBoard() {
            GridPane boardGrid = new GridPane();
    
            int tileNum = 8;
            double tileWidth = BORDER_WIDTH / tileNum;
            double tileHeight = BORDER_HEIGHT / tileNum;
    
            for (int i = 0; i < tileNum; i++) {
                for (int j = 0; j < tileNum; j++) {
                    Tile tile = new Tile(i, j);
                    // Set each 'Tile' the width and height
                    tile.setPrefSize(tileWidth, tileHeight);
                    // Add node on j column and i row
                    boardGrid.add(tile, j, i);
                }
            }
            // Return the GridPane
            return boardGrid;
        }
    
        class Tile extends Pane {
            private int positionX;
            private int positionY;
    
            public Tile(int x, int y) {
                positionX = x;
                positionY = y;
                setOnMouseClicked(e -> {
                    System.out.println(positionX + " " + positionY);
                });
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    从我的角度来看,如果你让类扩展窗格而不是仅仅引用窗格,你会更容易处理每个瓷砖,但这只是我的观点。不管怎样,上面只是一个例子。如果你找不到问题,那么发布一个MCVE节目,我们可以更好地帮助你。