java - 用数字和字母调整棋盘 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(大小)。如您所见,底部的字母缺失,右侧的数字缺失。我怎样才能添加它们?如果来自这里的代码: 具有自动调整大小的棋盘
解决方案
我不太明白你的评论,但你可以简单地添加另一列和另一行到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);
}
}
这是我运行它时的样子。
推荐阅读
- html - 不应该在移动菜单上显示的链接,在活动和悬停时
- angular - 如何在单个 index.html 文件中编译角度构建,该文件将从 http get 请求提供
- rust - 函数类型定义可以单独一行而不是内联吗?
- android - 将数据插入sqlite数据库时如何避免重复值?
- c# - 从 Azure 计算机视觉 API 提取文本时出错
- reactjs - 反应排除路线
- java - I want visible button has removed it text from TextView
- django - 来自芹菜任务延迟的Keyerror
- php - 使用 Google Optimize A/B 测试进行服务器端重定向
- security - eslint-utils 的安全漏洞,作为 eslint@5.12.0 的依赖项之一添加