java - 使用 JavaFX 的窗口内的棋盘

标签 java javafx javafx-8

我是新来的,我希望有人能好心帮助我。

我目前正在尝试创建一个跳棋游戏,因此需要创建棋盘。我已经检查过其他答案,但他们似乎从未在另一个窗口中创建棋盘(稍后我将在其中放置按钮以退出并开始新游戏)。现在,尽管我在循环中创建了多个矩形,但仅显示一个矩形。

有谁知道这是为什么吗? 这是我的代码

第一个:Checkersboard 类,它创建棋盘的矩形

package application;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;

public class CheckersBoard extends Pane {

private int boardWidth = 10;
private int boardHeight = 10;


private Rectangle[][] board;


public CheckersBoard() {
    // Declares new board
    board = new Rectangle[boardWidth][boardHeight];

    // Initializes new board
    for(int x=0; x < boardWidth; x++){
        for(int j=0; j < boardHeight; j++){
            board[x][j] = new Rectangle();
            board[x][j].setWidth(50);
            board[x][j].setHeight(50);
            board[x][j].setStroke(Color.TRANSPARENT);
            board[x][j].setStrokeType(StrokeType.INSIDE);
            board[x][j].setStrokeWidth(1);
        }
    }

    // Generates colours for the chessboard backgrounds
    for(int x=0; x < boardWidth; x++){
        for(int j=0; j < boardHeight; j++){
            if((x%2==0 && j%2==1) || (x%2==1 && j%2==0)){
            board[x][j].setFill(Color.PINK);
            }
            else if((x%2==0 && j%2==0) || (x%2==1 && j%2==1)){
            board[x][j].setFill(Color.DARKGRAY);
            }
        }
    }

}

public void placeboard(final int i, final int j){
    getChildren().add(board[i][j]);
}
}

二、将board添加到其他窗口然后显示的主类:

package application;

import javafx.application.Application; 
import javafx.stage.Stage;
import javafx.scene.Scene;
import .... ;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        CheckersBoard board = new CheckersBoard();

        GridPane root = (GridPane)FXMLLoader.load(getClass().getResource("/application/view/Base.fxml"));

        for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
                board.placeboard(i, j);
            }
        }
        root.add(board, 0, 0);
        Scene scene = new Scene(root,400,400);
              scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

最后是我的 fxml 文件“Base”,其网格 Pane 几乎为空:

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.StackPane?>

<GridPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Label text="test" GridPane.columnIndex="1" GridPane.rowIndex="2" />
   </children>
</GridPane>

谢谢大家的帮助! :)

亲切的问候,丽莎

最佳答案

所有矩形都位于(0,0)处。您需要设置xy坐标:

board[x][j].setX(x * 50); // 50 being the width of each rectangle
board[x][j].setY(y * 50); // 50 being the height of each rectangle

关于java - 使用 JavaFX 的窗口内的棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761825/

相关文章:

java - 无法统计应用程序。将 SpringBoot 从 2.0.6.RELEASE 更新到 2.1.0.RELEASE 后

java - Java 中的泛型特定接口(interface)定义

button - JavaFX Transition - 悬停时变暗按钮

JavaFx:自动调整 TitledPane 大小

css - 如何设置菜单按钮和菜单项的样式

java - 在外部 Android 存储中保存文件时出错

java - 调用 Intent 时应用程序关闭

Javafx 组合框重置问题

java - 创建一个带有函数参数的可重用按钮

JavaFx 可运行 JAR 导出不起作用