首页 > 解决方案 > 用数字和字母调整棋盘 JavaFX 的大小

问题描述

我正在尝试使用 javaFX 制作棋盘。我想看起来像这张图片: 在此处输入图片描述

这是我的代码:

package sample;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        final int size = 8 ;
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col ++) {
                StackPane square = new StackPane();
                String color ;
                if ((row + col) % 2 == 0) {
                    color = "white";
                } else {
                    color = "black";
                }
                square.setStyle("-fx-background-color: "+color+";");
                root.add(square, col, row);
            }
        }
        for (int i = 0; i < size; i++) {
            root.getColumnConstraints().add(new ColumnConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
            root.getRowConstraints().add(new RowConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true));
        }
        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在这个程序中,我定义了一个确定棋盘大小的常量(大小)。它也可以从 2 变为 16(大小)。如您所见,底部的字母缺失,右侧的数字缺失。我怎样才能添加它们?如果来自这里的代码: 具有自动调整大小的棋盘

标签: javajavafxchess

解决方案


我不太明白你的评论,但你可以简单地添加另一列和另一行到GridPane. 用数字填充第一列。用字母填充最后一行。

package sample;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane root = new GridPane();
        final int size = 8;
        for (int row = 0; row < size; row++) {
            for (int col = 0; col <= size; col ++) {
                StackPane square = new StackPane();
                if (col == 0) {
                    Label label = new Label(String.valueOf(row + 1));
                    square.getChildren().add(label);
                }
                else {
                    String color;
                    if ((row + col) % 2 == 0) {
                        color = "white";
                    }
                    else {
                        color = "black";
                    }
                    square.setStyle("-fx-background-color: "+color+";");
                }
                root.add(square, col, row);
            }
        }
        for (int i = 1; i <= size; i++) {
            StackPane square = new StackPane();
            char[] letter = new char[]{(char) ('A' + (i - 1))};
            Label label = new Label(new String(letter));
            square.getChildren().add(label);
            root.add(square, i, size);
        }
        for (int i = 0; i <= size; i++) {
            root.getColumnConstraints().add(new ColumnConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
            root.getRowConstraints().add(new RowConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true));
        }
        int dim = (size + 1) * 50;
        primaryStage.setScene(new Scene(root, dim, dim));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这是我运行它时的样子。

棋盘


推荐阅读