如何在具有两个场景的 JavaFXML 控制器中初始化圆形对象的双数组?

How do I initialize a double array of Circle Objects in a JavaFXML controller with two scenes?

我正在使用 JavaFXMLGluon Scene Builder 8.0.0 编写代码来创建像素编辑器应用程序。我创建了两个 .fxml 文件,一个用于绘图工具 (sample.fxml),另一个用于表示 LED 像素阵列 (PixelEditor.fxml) 的圆形对象 (32 x 32) 阵列。两者共享同一个控制器 (Controller.java)。

当用户单击 32h x 32w 等菜单项时,我无法在 Controller.java 中初始化我的 Circle[][] 数组。我使用 4 x4 数组来测试我的代码:

public void handleMenuAction(ActionEvent event) throws IOException {

   if(event.getSource() == menu32hx32w) {

  Stage pixelStage = new Stage();
  Parent pixelRoot = FXMLLoader.load(getClass().getResource("PixelEditor.fxml"));
  Scene pixelScene = new Scene(pixelRoot);
  pixelStage.setTitle("Pixel Array: 32h X 32w");
  pixelStage.setScene(pixelScene);
  pixelStage.setX(0.0);
  pixelStage.setY(0.0);
  pixelStage.show();

  Circle[][] pixelArray = {
       {R0C0, R0C1, R0C2, R0C3},
       {R1C0, R1C1, R1C2, R1C3},
       {R2C0, R2C1, R2C2, R2C3},
       {R3C0, R3C1, R3C2, R3C3},
    };
   }
}

如果我打印出我得到的数组:

pixelArray:
null null null null 
null null null null 
null null null null 
null null null null

当我只有一个包含所有对象的 .fxml 时,我可以初始化 pixelArray。我使用 fx:id 来引用圆形对象,但将它们放在单独的舞台和场景中似乎取消了对它们的引用并创建了一个空元素。

我没有做什么?

以前,使用一个 .fxml 文件,我需要为 Circle Objects 赋值是在 Controller.java 中引用它们的 fx:id,如下所示:

@FXML
private Circle
R0C0, R0C1, R0C2, R0C3,
R1C0, R1C1, R1C2, R1C3,
R2C0, R2C1, R2C2, R2C3,
R3C0, R3C1, R3C2, R3C3;

这就是我仍在做的事情,但是通过 fx:id 引用分配的属性似乎没有连接?

PixelEditor.fxml 相当大,因为我有 32x32 = 1024 个圆形对象,尽管我只是用第一个 4x4 进行测试。第一行的代码如下所示:

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

<?import javafx.geometry.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<Pane fx:id="panePixelLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="776.0" prefWidth="776.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <VBox prefHeight="776.0" prefWidth="776.0" style="-fx-background-color: #000000;">
            <children>
                <HBox prefHeight="24.0" prefWidth="776.0" style="-fx-background-color: #000000;">
                    <children>
                        <Circle fx:id="R0C0" fill="DODGERBLUE" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
                            <HBox.margin>
                                <Insets left="8.0" top="8.0" />
                            </HBox.margin>
                        </Circle>
                        <Circle fx:id="R0C1" fill="DODGERBLUE" layoutX="22.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
                            <HBox.margin>
                                <Insets left="8.0" top="8.0" />
                            </HBox.margin>
                        </Circle>
                        <Circle fx:id="R0C2" fill="DODGERBLUE" layoutX="22.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
                            <HBox.margin>
                                <Insets left="8.0" top="8.0" />
                            </HBox.margin>
                        </Circle>
                        <Circle fx:id="R0C3" fill="DODGERBLUE" layoutX="42.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
                            <HBox.margin>
                                <Insets left="8.0" top="8.0" />
                            </HBox.margin>
                        </Circle>

看起来您实际上并没有为以下任何一项设置值:R0C0R0C1 等。如果这些变量在您创建数组时设置为 null,即使您稍后设置它们,它们仍将是 null

您没有显示分配 R0C0 的代码部分,但很可能这就是问题所在。

jewelsea 在相关问题中回答了这个问题,jewelsea 在以下位置提供了最出色的答案:Have multiple FXML files (created in SceneBuilder), but only one controller. Does each scene load it's own copy of the controller?

通过创建两个控制器,我能够初始化我的 Circle[][] 数组,但现在我必须按照 jewelsea 通过提供的链接描述的那样在两个控制器之间传递参数。