gpt4 book ai didi

java - 如何删除动态添加的节点

转载 作者:行者123 更新时间:2023-12-02 02:48:12 24 4
gpt4 key购买 nike

我在 for 循环中生成了四个 HBox。每个HBox都有1个“删除按钮”。如何定位一个删除按钮来仅删除相应的HBox?我将所有 HBox 添加到名为 questionFieldVBox 中。

现在我正在使用:

private void removeAnswer() {
this.questionField.getChildren().removeAll(hBox);
}

但这会删除所有 4 个 HBox。我需要一些东西能够单独定位特定的 HBox

这是循环:

newAnswerTextField = new TextField[4];
answerCheckbox = new CheckBox[4];
ToggleGroup answerToggleGroup = new ToggleGroup();
radioButtonAnswer = new RadioButton[4];
hBox = new HBox[4];

for(int i = 0; i < 4; i++){
hBox[i] = new HBox();
Button deleteButton = new Button("X");
deleteButton.setOnAction(e -> {
removeAnswer();
});

newAnswerTextField[i] = new TextField();
newAnswerTextField[i].setText("Fråga " + questionNumber + " svar " + answerNumber++);
hBox[i].getChildren().addAll(deleteButton, newAnswerTextField[i]);

radioButtonAnswer[i] = new RadioButton("Rätt svar");
radioButtonAnswer[i].setToggleGroup(answerToggleGroup);
hBox[i].getChildren().add(radioButtonAnswer[i]);

questionField.getChildren().add(hBox[i]);
}

最佳答案

您可以简单地将要删除的 HBox 作为参数传递给您的 removeAnswer 方法:

private void removeAnswer(Node answer) {
this.questionField.getChildren().remove(answer);
}
for(int i = 0; i < 4; i++) {
final HBox answer = new HBox();
hBox[i] = answer;
Button deleteButton = new Button("X");
deleteButton.setOnAction(e -> {
removeAnswer(answer);
});

...
}

关于java - 如何删除动态添加的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44246286/

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