【问题标题】:Resizing Chess Board JavaFX with numbers and letters用数字和字母调整棋盘 JavaFX 的大小
【发布时间】:2021-06-20 06:31:17
【问题描述】:

我正在尝试使用 javaFX 制作棋盘。我想看起来像这张照片: enter image description here

这是我的代码:

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(大小)。如您所见,底部的字母缺失,右侧的数字缺失。我怎样才能添加它们?如果来自这里的代码: Chessboard with automatic resizing

【问题讨论】:

  • 您在问题中写道:底部的字母不见了您是否尝试在GridPane 中添加额外的一行?
  • 其实代码在主类上,场景构建器并没有显示棋盘。
  • 也许你应该提到你问题中的代码是从这个问题的接受答案复制而来的:Chessboard with automatic resizing

标签: java javafx chess


【解决方案1】:

我不太了解您的comment,但您可以简单地在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);
    }
}

这是我运行它时的样子。

【讨论】:

  • 代码正在运行,但是如果我将大小更改为 2,它将打印 a-h,如果大小为 2,我想从 a 打印到 b。同样,如果大小等于 8,则程序没有正确显示。我想要调整棋盘的大小。
  • @Danakas 当然,您可以修改代码来做到这一点。你不能指望使用这个网站完全照原样复制代码,并期望其他人做你需要的每一个微小的修改。
  • 那是很多重复的代码。执行String[] columnLabels = "ABCDEFGHIJKLMNOP".split("");,然后您可以在循环中创建列标签(并使其依赖于size,假设不大于16)。
  • @Danakas 我修改了答案中的代码。现在它适用于任何尺寸的棋盘。当然,如果大小大于 26,你会用完字母。
【解决方案2】:

更新! 我从以下答案中尝试了这段代码:

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 = 10 ;
        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);
            }
        }
        StackPane square = new StackPane();
            if(size==2) {
                Label label = new Label("A");
                square.getChildren().add(label);
                root.add(square, 1, 2);
                square = new StackPane();
                label = new Label("B");
                square.getChildren().add(label);
                root.add(square, 2, 2);
            }
        if(size==4) {
            Label label = new Label("A");
            square.getChildren().add(label);
            root.add(square, 1, 4);
            square = new StackPane();
            label = new Label("B");
            square.getChildren().add(label);
            root.add(square, 2, 4);
            square = new StackPane();
            label = new Label("C");
            square.getChildren().add(label);
            root.add(square, 3, 4);
            square = new StackPane();
            label = new Label("D");
            square.getChildren().add(label);
            root.add(square, 4, 4);
        }

        if(size==6){
            Label label = new Label("A");
            square.getChildren().add(label);
            root.add(square, 1, 6);
            square = new StackPane();
            label = new Label("B");
            square.getChildren().add(label);
            root.add(square, 2, 6);
            square = new StackPane();
            label = new Label("C");
            square.getChildren().add(label);
            root.add(square, 3, 6);
            square = new StackPane();
            label = new Label("D");
            square.getChildren().add(label);
            root.add(square, 4, 6);
            square = new StackPane();
            label = new Label("E");
            square.getChildren().add(label);
            root.add(square, 5, 6);
            square = new StackPane();
            label = new Label("F");
            square.getChildren().add(label);
            root.add(square, 6, 6);
        }

        if(size==8){
            Label label = new Label("A");
            square.getChildren().add(label);
            root.add(square, 1, 8);
            square = new StackPane();
            label = new Label("B");
            square.getChildren().add(label);
            root.add(square, 2, 8);
            square = new StackPane();
            label = new Label("C");
            square.getChildren().add(label);
            root.add(square, 3, 8);
            square = new StackPane();
            label = new Label("D");
            square.getChildren().add(label);
            root.add(square, 4, 8);
            square = new StackPane();
            label = new Label("E");
            square.getChildren().add(label);
            root.add(square, 5, 8);
            square = new StackPane();
            label = new Label("F");
            square.getChildren().add(label);
            root.add(square, 6, 8);
            square = new StackPane();
            label = new Label("G");
            square.getChildren().add(label);
            root.add(square, 7, 8);
            square = new StackPane();
            label = new Label("H");
            square.getChildren().add(label);
            root.add(square, 8, 8);
        }

        if(size==10){
            Label label = new Label("A");
            square.getChildren().add(label);
            root.add(square, 1, 10);
            square = new StackPane();
            label = new Label("B");
            square.getChildren().add(label);
            root.add(square, 2, 10);
            square = new StackPane();
            label = new Label("C");
            square.getChildren().add(label);
            root.add(square, 3, 10);
            square = new StackPane();
            label = new Label("D");
            square.getChildren().add(label);
            root.add(square, 4, 10);
            square = new StackPane();
            label = new Label("E");
            square.getChildren().add(label);
            root.add(square, 5, 10);
            square = new StackPane();
            label = new Label("F");
            square.getChildren().add(label);
            root.add(square, 6, 10);
            square = new StackPane();
            label = new Label("G");
            square.getChildren().add(label);
            root.add(square, 7, 10);
            square = new StackPane();
            label = new Label("H");
            square.getChildren().add(label);
            root.add(square, 8, 10);
            square = new StackPane();
            label = new Label("I");
            square.getChildren().add(label);
            root.add(square, 9, 10);
            square = new StackPane();
            label = new Label("J");
            square.getChildren().add(label);
            root.add(square, 10, 10);
        }


        for (int i = 0; i <= size; i++) {
            root.getRowConstraints().add(new RowConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true));
            root.getColumnConstraints().add(new ColumnConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));

        }
        primaryStage.setScene(new Scene(root, 450, 400));
        primaryStage.show();
    }

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

它正在工作,但这是一个好方法吗?

【讨论】:

    猜你喜欢
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    相关资源
    最近更新 更多