窗格未在场景中显示矩形网格

问题描述

package com.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import org.w3c.dom.Text;

import java.net.URL;
import java.util.ResourceBundle;

public class LayoutController {
    @FXML
    Pane boardPane;

    @FXML
    Button btnPlayAgain;

    @FXML
    TextField message;

    public void btnPlayAgain(ActionEvent actionEvent) {
        message.setText("Testing...");
    }

    @FXML
    public void initialize() {
        message.setText("Working");
        int w = (int) boardPane.getWidth();
        int h = (int) boardPane.getHeight();
        int grid = 12;
        int scaleW = w / grid;
        int scaleH = h / grid + 5;

        for (int i = 0; i < w; i += scaleW) {
            for (int j = 0; j < h; j += scaleH) {
                Rectangle rectangle = new Rectangle(i,j,scaleW,scaleH);
                rectangle.setFill(Color.LIGHTYELLOW);
                rectangle.setopacity(2);
                rectangle.setstroke(Color.BLACK);
                boardPane.getChildren().add(rectangle);
            }
        }
    }
}

我的 FXML :

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane minHeight="300" minWidth="300" prefheight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gui.LayoutController">
    <Pane prefheight="50.0" prefWidth="600.0">
        <TextField layoutX="100.0" layoutY="14.0" />
        <TextField layoutX="375.0" layoutY="14.0" />
        <Label layoutX="285.0" layoutY="15.0" text="Player 2" textFill="#000dff">
            <font>
                <Font name="System Bold" size="17.0" />
            </font>
        </Label>

        <Label alignment="TOP_LEFT" contentdisplay="RIGHT" layoutX="10.0" layoutY="15.0" text="Player 1" textFill="#ee0303">
            <font>
                <Font name="System Bold" size="17.0" />
            </font>
        </Label>
    </Pane>

    <Pane fx:id="boardPane" layoutY="50.0" minHeight="300.0" minWidth="600.0" prefheight="300.0" prefWidth="600.0" />

    <Pane fx:id="paneBottom" layoutY="354.0" prefheight="42.0" prefWidth="600.0">
        <Button fx:id="btnPlayAgain" layoutX="5.0" layoutY="10.0" mnemonicParsing="false" onAction="#btnPlayAgain" text="Play Again" />
        <TextField fx:id="message" layoutX="100.0" layoutY="10.0" prefWidth="450.0" editable="false"/>
    </Pane>
</AnchorPane>

它应该显示如下内容

Image 1

但它显示出类似的东西:

Image 2

请帮助任何人都可以帮助... :)

我想每次运行代码时都显示网格,但窗格布局中没有显示任何内容我尝试了不同的时间,但似乎没有任何效果 您对我的问题的洞察将非常有帮助...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)