Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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_Javafx_Javafx 2 - Fatal编程技术网

JavaFX:从组中删除文本会导致异常

JavaFX:从组中删除文本会导致异常,java,javafx,javafx-2,Java,Javafx,Javafx 2,我将图像视图和文本放在组中。当我尝试删除文本时,出现以下异常: Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException at com.sun.javafx.collections.VetoableListDecorator$VetoableIteratorDecorator.checkForComodification(Unknown Source) at

我将
图像视图
文本
放在
中。当我尝试删除
文本
时,出现以下异常:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
    at com.sun.javafx.collections.VetoableListDecorator$VetoableIteratorDecorator.checkForComodification(Unknown Source)
    at com.sun.javafx.collections.VetoableListDecorator$VetoableIteratorDecorator.hasNext(Unknown Source)
    at fjr.tool.TestMake$2$2$1.handle(TestMake.java:67)
    at fjr.tool.TestMake$2$2$1.handle(TestMake.java:1)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
这是我的代码:

package fjr.tool;

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TestMake extends Application{

    int num  = 2; 
    Group[] groupCollection = new Group[num];
    ImageView view[] = new ImageView[num]; 

    String url[] = new String[]{
            "E:/testimage/1.jpg", 
            "E:/testimage/2.jpg"
    };

    @Override
    public void start(Stage stage) throws Exception {
        Group root = new Group(); 
        stage.setScene(new Scene(root, 500, 400));  

        HBox box = new HBox(){{
            setSpacing(10); 
        }};



        box.getChildren().addAll(new VBox(){{
            setSpacing(5); 
            getChildren().addAll(new  Button("ADD LABEL"){{
                setPrefWidth(100);
                setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {  
                        int yy = 0 ; 
                        for(int i=0; i< num; i++){
                            double width = view[i].getImage().getWidth(); 
                            Text text = new Text("AA"); 
                            text.setTranslateX(width - 20);
                            text.setTranslateY(yy); 
                            groupCollection[i].getChildren().add(text); 
                            yy += view[i].getImage().getHeight(); 
                        }
                    }
                });
            }}, new Button("REMOVE LABEL"){{
                setPrefWidth(100); 
                setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {  
                        for(int i=0; i< num;i++){
                            Group g = groupCollection[i]; 
                            for(Node node : g.getChildren()){
                                if(node instanceof Text){
                                    g.getChildren().remove(node); 
                                }
                            }
                        }
                    }
                });
            }}); 
        }}, 

        new ScrollPane(){{
            setPrefSize(370, 300);
            setContent( new VBox(){{
                setSpacing(5);
                for(int i=0; i< num; i++){
                    final int j = i; 
                    groupCollection[i] = new Group(view[i] = new ImageView() {{
                        setImage(new Image(new File(url[j]).toURI().toString()));
                    }}); 
                    getChildren().add(groupCollection[i]); 
                }

            }});
        }}
                ); 

        root.getChildren().add(box); 
        stage.show(); 
    }

    public static void main(String[] args){
        launch(args); 
    }
}
Packagefjr.tool;
导入java.io.File;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.Group;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ScrollPane;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.text;
导入javafx.stage.stage;
公共类TestMake扩展了应用程序{
int num=2;
组[]组集合=新组[num];
ImageView视图[]=新的ImageView[num];
字符串url[]=新字符串[]{
“E:/testimage/1.jpg”,
“E:/testimage/2.jpg”
};
@凌驾
public void start(Stage)引发异常{
组根=新组();
舞台场景(新场景(根,500,400));
HBox box=新的HBox(){{
起搏器(10);
}};
box.getChildren().addAll(新的VBox()文件){{
起搏器(5);
getChildren().addAll(新建按钮(“添加标签”){{
设置宽度(100);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent arg0){
int-yy=0;
for(int i=0;i
迭代列表时,不能直接修改列表。迭代器被搞糊涂了


我不太确定哪一个是最好的;第二个版本(使用iterator.remove())只通过子列表一次,但每次删除节点时都会调用列表的侦听器。(这并不像听起来那么糟糕;它只是被标记为无效,然后在下一个脉冲中计算一次布局)。第一个和第三个基本上通过列表进行两次传递,一次标记要删除的节点,另一次删除它们;但是,在批量删除时,侦听器只收到一次通知。
List<Node> nodesToRemove = new ArrayList<>();
for(Node node : g.getChildren()){
    if(node instanceof Text){
        nodesToRemove.add(node);
    }
}

g.getChildren().removeAll(nodesToRemove);
for (Iterator<Node> iterator = g.getChildren().iterator(); iterator.hasNext();) {
    if (iterator.next() instanceof Text) {
        iterator.remove();
    }
}
g.getChildren().removeIf(node -> node instanceof Text);