如何在控制台输出上删除java.awt.Point [...]

时间:2015-12-05 02:52:09

标签: java

我正在使用java.awt.Point,当我尝试返回Point个对象时,会打印出java.awt.Point [x =.., y=...]。如何摆脱java.awt.Point,让方法只返回xy

1 个答案:

答案 0 :(得分:1)

我不知道你的问题是否仍然存在(我希望不是,你找到了解决方案=))
但以防万一:
当您向字符串写入someObject.getLocation()之类的内容时,您将添加一个Point对象。这意味着它将toString()方法转换为完全用于java.awt.Point的字符串,如下所示:

public String toString() {
return "java.awt.Point[x=" + x + ",y=" + y + "]";

}

但你可以编写自己的,替代toString(),如:

// Get the object's location.
Point location = someObject.getLocation();

// Here goes your method
String locationString = "[" + location.x + "," + location.y + "]";

// Then just print it
System.out.println(lcoationString);