gpt4 book ai didi

java - 不能使用root.getchildren(),只能使用root.getchildrenunmodifying()

转载 作者:行者123 更新时间:2023-11-30 01:42:55 26 4
gpt4 key购买 nike

上下文

我正在为一个小游戏创建一个 GUI。游戏有一个开始屏幕。当玩家点击开始时,舞台场景从菜单场景变为游戏场景。新的游戏场景以 Group 作为父级,并包含一些元素,例如玩家的得分和姓名,所有这些元素都已在 FXML 文件中定义。到目前为止,该程序可以运行。

问题

我现在的目标是创建一个网格,它将作为我的游戏的基础。当玩家从菜单开始游戏时,该网格应该出现在游戏场景中。后来我希望能够根据用户输入更改板的大小

我尝试了什么

<强>1。在事件处理程序中创建网格

我的理由是在玩家单击启动游戏的“单人游戏”按钮时创建网格。按照这个推理,我只需获取根节点(一组)的子节点并将网格 Pane 添加到其中

为什么我陷入困境

以下代码片段显示处理按钮单击事件的 Controller 。注释掉后,我创建了一个按钮,获取了根节点并使用标准 getChildren().add(...) 方法。然而这失败了。 Intellij 实际上建议使用 getChildrenUnmodifying() 来代替。根据我的理解, getChildrenUnmodifying() 是只读的,这不符合我的目的。我读到问题是父项受到保护且无法修改。那我应该去哪里修改呢?

@FXML
public void onButtonChangeScene(ActionEvent event) throws IOException {
// Go to single player window
if (event.getSource().equals(singleplayer)){
Parent root = FXMLLoader.load(getClass().getResource("Views/board.fxml"));
Scene rootscene = new Scene(root);

//Get stage information
Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
window.setScene(rootscene);


// Creates the board
Button button1 = new Button("Button 1");
//Part that fails
root.getchildren().add(button1);

window.show();

fxml 文件如下所示

Group xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.BoardController">
<children>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0">
<left>
<AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutY="6.0" prefHeight="17.0" prefWidth="200.0" text="Player 1">
<font>
<Font size="30.0" />
</font>
</Label>
<Label alignment="CENTER" contentDisplay="CENTER" layoutY="51.0" prefHeight="17.0" prefWidth="200.0" text="1200">
<font>
<Font size="30.0" />
</font>
</Label>
<Button fx:id="abandon" layoutX="71.0" layoutY="342.0" mnemonicParsing="false" onAction="#onButtonChangeScene" text="Abandon" />
</children>
</AnchorPane>
</left>
<right>
<AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutY="7.0" prefHeight="17.0" prefWidth="200.0" text="Player 2">
<font>
<Font size="30.0" />
</font>
</Label>
<Label alignment="CENTER" contentDisplay="CENTER" layoutY="52.0" prefHeight="17.0" prefWidth="200.0" text="800">
<font>
<Font size="30.0" />
</font>
</Label>
</children>
</AnchorPane>
</right>
<center>
<GridPane fx:id="gridpane" BorderPane.alignment="CENTER">
<columnConstraints>
<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>
<ImageView fitHeight="118.0" fitWidth="99.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Images/bluecard.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.rowIndex="1" />
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.rowIndex="2" />
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
</center>
</BorderPane>
</children>
</Group>

<强>2。在 fxml 中静态创建网格

我尝试使用 scenebuilder 在 FXML 中创建网格 Pane 。但是我不确定如何使用代码与 fxml 交互来修改网格 Pane 的大小或内容。

问题

1 将 GridPane 添加到父节点的最佳方式是什么

a) 在事件处理程序中创建它。

b) 在FXML文件中静态创建,并修改板子的大小

2 如何解决root.getChildren()能够添加节点的问题?

最佳答案

您需要将 .add() 与 Pane 一起使用,并且您的根目录不是 Pane。

尝试

private BorderPane mainpane = (BorderPane)root.getchildren();
mainpane.add(gridpane);

将 fx:id 设置为要添加 gridPane 的 Pane ,例如:

<BorderPane fx:id = "mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0">

然后你可以这样做:

private BorderPane mainpane;
private GridPane gridpane;
mainpane.add(gridpane);

关于java - 不能使用root.getchildren(),只能使用root.getchildrenunmodifying(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59305468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com