我正在使用JavaFXML
和Gluon Scene Builder 8.0.0
进行编码以创建像素编辑器应用程序。我创建了两个. fxml文件,一个用于绘图工具(sample.fxml
),另一个用于表示像素LED阵列(PixelEditor.fxml
)的圆形对象数组(32 x 32)。两者共享相同的控制器(Controller.java
)。
当用户单击像< code>32h x 32w这样的菜单项时,我无法在< code>Controller.java中初始化我的< code>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文件,我只需在Controller.java中引用它们的fx: id即可为Circle Object赋值,如下所示:
@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>
看起来您实际上没有将值设置为以下任何值:R0C0
、R0C1
等。如果在创建数组时将这些变量设置为null
,即使您稍后设置它们,它们仍然是null
。
您还没有显示代码中分配R0C0
的部分,但很可能是问题所在。
jewelsea在一个相关问题中回答了这个问题,他在以下网站上提供了最出色的答案:拥有多个FXML文件(在SceneBuilder中创建),但只有一个控制器。每个场景是否加载它自己的控制器副本?。
通过创建两个控制器,我能够初始化我的 Circle[][] 数组,但现在我必须按照 jewelsea 通过提供的链接描述在两个控制器之间传递参数。