【发布时间】:2017-10-19 20:33:04
【问题描述】:
我从存储在单独行的文本文件中读取对象(实际上是字符串,但我将它们转换为对象)。
编辑:我的问题是,当对象被添加到面板时,它们会相互重叠。这些对象有自己的独立 X 和 Y 坐标,它们应该被绘制在其中。
像这样(这是我从中读取的保存的文本文件):
Described,Bus,182,73,PlaceWithDesc,random desc
Named,Bus,53,31,Place1
Named,train,84,100,Place2
tokens[0] 只是一个硬编码字符串
[1] 是一种运输类别(它是一个对象,当它在图片面板上绘制时,它会赋予该地点的三角形颜色)
[2] 是对象 Position 的 x 坐标,[3] 是 Y 坐标。
[4] 是构建对象时的 Place 名称。
如果 [0] 等于 Described,则还会创建带有描述的地方。
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();
现在我的问题是,当我添加 Place 对象时,它们会相互重叠。请注意,NamedPlace 和 DescribedPlace 是超类 Place 的子类。
编辑:我通过执行 picturePanel.repaint(); 解决了它和picturePanel.validate();
【问题讨论】:
-
可能是布局覆盖了您设置的“边界”值。看一看:stackoverflow.com/questions/17264761/…
标签: java