如何在 FXML 中将节点添加到另一个节点的子节点?
How to add node to another node's child in FXML?
我想要得到的示例,用 Java 代码编写:
public class Main extends Application {
private static Scene scene;
public static void main(String[] args) {
launch(args);
}
@Override
public void init() throws IOException {
// Load root pane from FXML file.
URL url = getClass().getResource("sample.fxml");
StackPane root = FXMLLoader.load(url);
// Create scene for a root node on JavaFX thread.
Platform.runLater(() -> scene = new Scene(root, 600, 400));
}
@Override
public void start(Stage stage) {
stage.setScene(scene);
stage.show();
}
}
自定义节点:
public class CustomGroup extends Group {
private VBox contentPane = new VBox();
public CustomGroup() {
getChildren().add(contentPane);
contentPane.getChildren().add(new Label("First Label"));
contentPane.getChildren().add(new Label("Second Label"));
}
}
FXML:
<StackPane>
<CustomGroup/>
</StackPane>
上面的代码是我想要获得的示例,但我不想在 Java 代码中添加标签,而是想在 FXML 中添加它们。类似的东西:
<StackPane>
<CustomGroup>
<Label text="First Label"/>
<Label text="Second Label"/>
</CustomGroup>
</StackPane>
但这会向自定义组添加标签。我想将它们添加到自定义组的内容窗格 (VBox)。
不过,我不确定你为什么要在 CustomGroup
的构造函数中将标签添加到 VBox 中,我会忽略它并回答你的问题。
您可以添加一个单独的方法来将项目添加到您的 VBox
。让我们考虑以下方法:
setItems()
接受 Nodes
将它们添加到 VBox
getItems()
其中 returns 来自 VBox 的 ObservableList<Node>
自定义组
public class CustomGroup extends Group {
private VBox contentPane = new VBox();
public CustomGroup() {
getChildren().add(contentPane);
contentPane.getChildren().add(new Label("First Label"));
contentPane.getChildren().add(new Label("Second Label"));
}
public void setItems(Node...nodes) {
contentPane.getChildren().addAll(nodes);
}
public ObservableList<Node> getItems() {
return contentPane.getChildren();
}
}
FXML
<CustomGroup>
<items>
<Button text="hi"/>
</items>
</CustomGroup>
此 FXML 在 VBox
.
中添加了新的 Button
我想要得到的示例,用 Java 代码编写:
public class Main extends Application {
private static Scene scene;
public static void main(String[] args) {
launch(args);
}
@Override
public void init() throws IOException {
// Load root pane from FXML file.
URL url = getClass().getResource("sample.fxml");
StackPane root = FXMLLoader.load(url);
// Create scene for a root node on JavaFX thread.
Platform.runLater(() -> scene = new Scene(root, 600, 400));
}
@Override
public void start(Stage stage) {
stage.setScene(scene);
stage.show();
}
}
自定义节点:
public class CustomGroup extends Group {
private VBox contentPane = new VBox();
public CustomGroup() {
getChildren().add(contentPane);
contentPane.getChildren().add(new Label("First Label"));
contentPane.getChildren().add(new Label("Second Label"));
}
}
FXML:
<StackPane>
<CustomGroup/>
</StackPane>
上面的代码是我想要获得的示例,但我不想在 Java 代码中添加标签,而是想在 FXML 中添加它们。类似的东西:
<StackPane>
<CustomGroup>
<Label text="First Label"/>
<Label text="Second Label"/>
</CustomGroup>
</StackPane>
但这会向自定义组添加标签。我想将它们添加到自定义组的内容窗格 (VBox)。
不过,我不确定你为什么要在 CustomGroup
的构造函数中将标签添加到 VBox 中,我会忽略它并回答你的问题。
您可以添加一个单独的方法来将项目添加到您的 VBox
。让我们考虑以下方法:
setItems()
接受Nodes
将它们添加到 VBoxgetItems()
其中 returns 来自 VBox 的
ObservableList<Node>
自定义组
public class CustomGroup extends Group {
private VBox contentPane = new VBox();
public CustomGroup() {
getChildren().add(contentPane);
contentPane.getChildren().add(new Label("First Label"));
contentPane.getChildren().add(new Label("Second Label"));
}
public void setItems(Node...nodes) {
contentPane.getChildren().addAll(nodes);
}
public ObservableList<Node> getItems() {
return contentPane.getChildren();
}
}
FXML
<CustomGroup>
<items>
<Button text="hi"/>
</items>
</CustomGroup>
此 FXML 在 VBox
.