[英]JavaFX chessboard build, set gridpane cell color
我做了這個二維數組。 我想你會明白的。
public int[][] table = new int[8][8] ;
public void initTable()
{
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i % 2 != 0)
{
if(j % 2 == 0) {
table[i][j] = 1; //black field
}
}
else if (i % 2 == 0)
{
if(j % 2 != 0)
{
table[i][j] = 1;
}
}
}
}
}
我想為網格窗格單元格着色。 我試過這個:
public class GameController {
@FXML
private GridPane gridPane;
@FXML
private Pane blackPane = new Pane();
@FXML
private Pane whitePane = new Pane();;
private final Game game = new Game();
public void initGame () {
blackPane.setStyle("-fx-background-color: #454343;");
whitePane.setStyle("-fx-background-color: #fafafa;");
System.out.println("PRESSED");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (game.table[i][j] == 1) {
gridPane.add(blackPane, i, j);
}
if (game.table[i][j] == 0) {
gridPane.add(whitePane, i, j);
}
}
}
}
}
當我按下按鈕時,它調用 function,打印出“PRESSED”,但不要將背景顏色更改為黑色或白色。
可能是什么問題?
我做了一個小例子,你怎么能做到。 在分析我的示例時,您可能會發現代碼中的錯誤。
Controller Class:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private GridPane gameFieldGridPane;
// Choose what u need:
private Pane[][] paneArray;
private List<Pane> paneList;
@Override
public void initialize(URL location, ResourceBundle resources) {
paneList = new ArrayList<>();
paneArray = new Pane[8][8];
}
@FXML
public void handleStartBtnClick() {
System.out.println("PRESSED");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Pane pane = new Pane();
paneList.add(pane);
paneArray[i][j] = pane;
String blueStyle = "-fx-background-color: blue;",
redStyle = "-fx-background-color: red;";
if (i % 2 == 0) {
if (j % 2 == 0) {
pane.setStyle(redStyle);
} else {
pane.setStyle(blueStyle);
}
} else {
if (j % 2 != 0) {
pane.setStyle(redStyle);
} else {
pane.setStyle(blueStyle);
}
}
gameFieldGridPane.add(pane, j, i);
}
}
}
@FXML
public void handleMakeBlackBtnClick() {
changeColorToBlack(paneArray[2][2]);
// or equivalent:
// changeColorToBlack(paneList.get(18));
}
private void changeColorToBlack(Pane pane) {
if (pane == null) return;
pane.setStyle("-fx-background-color: black;");
}
}
FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="3.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<GridPane fx:id="gameFieldGridPane">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<Button onAction="#handleStartBtnClick" text="Start" />
<Button onAction="#handleMakeBlackBtnClick" text="Make one Pane black" />
</children>
<padding>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
</padding>
</VBox>
預習:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.