长话短说,我有8x8 GridPane(将其用作国际象棋棋盘),我希望能够单击每个单元格并获取其坐标。

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”,而不是实际的行/列位置。

最佳答案

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


您尚未在每个窗格(标题)上指定具体的大小(宽度,高度)
我猜您在某个地方设置了GridPane的大小,但这只是一个猜测,现在,我不建议您在Grid上添加背景图像的方式改为使用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);
    }
}


从我的角度来看,如果您使类扩展Pane而不是仅持有对它的引用,那么处理每个Tile会更容易,但这只是我的观点。好吧,以上仅仅是一个示例。如果您找不到问题,请发布MCVE节目,我们可以为您提供更好的帮助。

关于java - 在GridPane JavaFx中选择单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50900317/

10-10 20:24