Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javafx中动态加载和删除节点?_Java_User Interface_Javafx - Fatal编程技术网

如何在javafx中动态加载和删除节点?

如何在javafx中动态加载和删除节点?,java,user-interface,javafx,Java,User Interface,Javafx,我需要动态加载按钮列表,并根据用户的决定将ActionListner添加到所有按钮列表中。接下来,我必须删除这个按钮,并加载其他按钮,而不是它们。我试着用这个密码 public void clearRadioButtons() { for(int i =0; i< radioButtons.size() -1;i++) { radioButtons.remove(i); group.getToggles().remove(i);

我需要动态加载按钮列表,并根据用户的决定将ActionListner添加到所有按钮列表中。接下来,我必须删除这个按钮,并加载其他按钮,而不是它们。我试着用这个密码

public void clearRadioButtons()
{


    for(int i =0; i< radioButtons.size() -1;i++)
    {
        radioButtons.remove(i);
        group.getToggles().remove(i);
        vBox.getChildren().remove(radioButtons);

    }
    radioButtons.clear();
    group.getToggles().clear();
    System.out.println("Clear"+radioButtons.size());


}
public void clearRadioButtons()
{
对于(int i=0;i

但是这些单选按钮仍然在我的屏幕上。我不能删除它们。有人知道如何动态加载、删除和加载某个元素而不是它们吗?

List
remove(Object)
方法仅在作为参数传递的对象是列表的一部分时删除元素。传递列表不会导致修改列表

此外,循环的实现也不正确(除非您不想删除最后一个
单选按钮
并跳过其他单选按钮)

使用
removeAll(Collection)
来删除单选按钮。使用
clear
也可能是一个选项,除非有
VBox
的其他子项

示例:

private final List<RadioButton> radioButtons = new ArrayList<>();
private final ToggleGroup toggleGroup = new ToggleGroup();
private VBox vBox;

private void clearRadios() {
    vBox.getChildren().removeAll(radioButtons); // remove from scene
    radioButtons.clear(); // remove from list
    toggleGroup.getToggles().clear(); // remove from ToggleGroup
}

private void addRadios(int count) {
    for (int i = 0; i < count; i++) {
        RadioButton radio = new RadioButton();
        radio.setToggleGroup(toggleGroup);
        radioButtons.add(radio);
        vBox.getChildren().add(radio);
    }
}

@Override
public void start(Stage stage) throws Exception {
    Button btn = new Button("add");
    btn.setOnAction(evt -> addRadios(5));

    Button btn2 = new Button("clear");
    btn2.setOnAction(evt -> clearRadios());

    vBox = new VBox(btn, btn2);

    stage.setScene(new Scene(vBox, 300, 500));
    stage.show();
}
private final List单选按钮=new ArrayList();
private final ToggleGroup ToggleGroup=新ToggleGroup();
专用VBox VBox;
专用无线电设备(){
vBox.getChildren().removeAll(单选按钮);//从场景中删除
单选按钮。清除();//从列表中删除
toggleGroup.getToggles().clear();//从toggleGroup中删除
}
专用void addradio(整数计数){
for(int i=0;i添加无线电(5));
按钮btn2=新按钮(“清除”);
设置操作(evt->clearRadios());
vBox=新的vBox(btn,btn2);
舞台场景(新场景(vBox,300500));
stage.show();
}