Java 从文本文件读取对象时处理错误,为什么?

Java 从文本文件读取对象时处理错误,为什么?,java,Java,我从文本文件中读取对象(实际上是字符串,但我将它们转换为对象),这些文本文件存储在单独的行中 编辑:我的问题是,当对象添加到面板时,它们会在彼此的顶部绘制出来。这些对象有各自独立的X和Y坐标,它们应该被绘制进去 像这样(这是我从中读取的保存的文本文件): 令牌[0]只是一个硬编码字符串 [1] 是交通工具的一个类别(它是一个对象,当它在图片面板上绘制时,为该位置提供三角形的颜色) [2] 是对象位置的x坐标,[3]是Y坐标 [4] 对象构造时的位置名称 如果[0]等于description,则还

我从文本文件中读取对象(实际上是字符串,但我将它们转换为对象),这些文本文件存储在单独的行中

编辑:我的问题是,当对象添加到面板时,它们会在彼此的顶部绘制出来。这些对象有各自独立的X和Y坐标,它们应该被绘制进去

像这样(这是我从中读取的保存的文本文件):

令牌[0]只是一个硬编码字符串

[1] 是交通工具的一个类别(它是一个对象,当它在图片面板上绘制时,为该位置提供三角形的颜色)

[2] 是对象位置的x坐标,[3]是Y坐标

[4] 对象构造时的位置名称

如果[0]等于description,则还将使用description创建位置

            FileReader inFile = new FileReader("place.reg");
            BufferedReader in = new BufferedReader(inFile);

            String line;
            while ((line = in.readLine()) != null) {
                String[] tokens = line.split(",");  
                Category category = null;
                String categoryName = tokens[1];

                if (categoryName.equals("Bus")) {
                    category = transportCategory[0];
                } else if (categoryName.equals("Underground")) {
                    category = transportCategory[1];
                } else if (categoryName.equals("Train")) {
                    category = transportCategory[2];
                } else if (categoryName.equals("None")) {
                    category = transportCategory[3];
                }

                String placeName = tokens[0];
                int x = Integer.parseInt(tokens[2]);
                int y = Integer.parseInt(tokens[3]);
                Position pos = new Position(x, y);
                String name = tokens[4];

                if (placeName.equals("Named")) {
                    NamedPlace nPlace = new NamedPlace(pos, category, name);

                    add(nPlace);
                    picturePanel.add(nPlace);
                    nPlace.addMouseListener(placeMouseLis);

                    System.out.println("named place added: " + 
nPlace.position().getX() + " , " + pos.getX());
                } else if (placeName.equals("Described")) {
                    String description = tokens[5];
                    DescribedPlace dPlace = new DescribedPlace(pos, 
category, name, description);

                    add(dPlace);
                    picturePanel.add(dPlace);

                    System.out.println("desc place was added");
                }
            }
                 //  Iterator<Map.Entry<Position, Place>> iterator = 
                 //  allPlaces.entrySet().iterator();
                 //  while(iterator.hasNext()){
                 //  Place place = iterator.next().getValue();
                 //  picturePanel.add(place);
                }
                inFile.close();
                in.close();
FileReader infle=newfilereader(“place.reg”);
BufferedReader in=新的BufferedReader(填充);
弦线;
而((line=in.readLine())!=null){
String[]tokens=line.split(“,”);
类别=空;
字符串categoryName=tokens[1];
if(类别名称等于(“总线”)){
类别=运输类别[0];
}else if(categoryName.equals(“地下”)){
类别=运输类别[1];
}else if(categoryName.equals(“Train”)){
类别=运输类别[2];
}else if(categoryName.equals(“无”)){
类别=运输类别[3];
}
字符串placeName=tokens[0];
intx=Integer.parseInt(标记[2]);
inty=Integer.parseInt(标记[3]);
位置pos=新位置(x,y);
字符串名称=令牌[4];
if(placeName.equals(“Named”)){
NamedPlace nPlace=新的NamedPlace(位置、类别、名称);
添加(nPlace);
picturePanel.add(nPlace);
nPlace.addMouseListener(placeMouseLis);
System.out.println(“添加的命名位置:”+
nPlace.position().getX()+”,“+pos.getX());
}else if(placeName.equals(“已描述”)){
字符串描述=令牌[5];
描述地点dPlace=新描述地点(位置,
类别、名称、说明);
添加(dPlace);
picturePanel.add(dPlace);
System.out.println(“添加了描述位置”);
}
}
//迭代器迭代器=
//entrySet().iterator();
//while(iterator.hasNext()){
//Place=iterator.next().getValue();
//图片面板。添加(位置);
}
infle.close();
in.close();
现在我的问题是,当我添加Place对象时,它们会在彼此的顶部绘制出来。请注意,NamedPlace和DescribedPlace是超类Place的子类


编辑:我通过执行picturePanel.repaint()解决了这个问题;和picturePanel.validate()

我是最大的白痴。。我忘记重新油漆了;并验证();在我的picturePanel上读取所有对象后。

可能是布局覆盖了您设置的“边界”值。看一看:
            FileReader inFile = new FileReader("place.reg");
            BufferedReader in = new BufferedReader(inFile);

            String line;
            while ((line = in.readLine()) != null) {
                String[] tokens = line.split(",");  
                Category category = null;
                String categoryName = tokens[1];

                if (categoryName.equals("Bus")) {
                    category = transportCategory[0];
                } else if (categoryName.equals("Underground")) {
                    category = transportCategory[1];
                } else if (categoryName.equals("Train")) {
                    category = transportCategory[2];
                } else if (categoryName.equals("None")) {
                    category = transportCategory[3];
                }

                String placeName = tokens[0];
                int x = Integer.parseInt(tokens[2]);
                int y = Integer.parseInt(tokens[3]);
                Position pos = new Position(x, y);
                String name = tokens[4];

                if (placeName.equals("Named")) {
                    NamedPlace nPlace = new NamedPlace(pos, category, name);

                    add(nPlace);
                    picturePanel.add(nPlace);
                    nPlace.addMouseListener(placeMouseLis);

                    System.out.println("named place added: " + 
nPlace.position().getX() + " , " + pos.getX());
                } else if (placeName.equals("Described")) {
                    String description = tokens[5];
                    DescribedPlace dPlace = new DescribedPlace(pos, 
category, name, description);

                    add(dPlace);
                    picturePanel.add(dPlace);

                    System.out.println("desc place was added");
                }
            }
                 //  Iterator<Map.Entry<Position, Place>> iterator = 
                 //  allPlaces.entrySet().iterator();
                 //  while(iterator.hasNext()){
                 //  Place place = iterator.next().getValue();
                 //  picturePanel.add(place);
                }
                inFile.close();
                in.close();