Netbeans 8.2 Java:无法编译的代码试图访问 java.awt.Shape.contains()
Netbeans 8.2 Java: Uncompilable code trying to access java.awt.Shape.contains()
在 Java 中使用 Netbeans 8.2,我遇到了一个我无法理解的错误。
我正在尝试检查是否单击了 Shape 对象,然后将其从我的 Shape 对象列表中删除(因此使用了迭代器)。但是有些东西导致了一个问题,阻止 Shape.contain(Point p) 工作,给我这个错误信息:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Iterator.contains
...
这里有什么问题? contains() 不应该像这样工作吗?我错过了什么?
完整代码:
public DuckHuntPanel() {
setBackground(Color.BLACK);
shapes = new ArrayList<>();
shapes.add(ball);
Timer timer = new Timer(1000 / 60, (ActionListener) this);
timer.start();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
super.mouseClicked(me);
Iterator<Shape> shape = shapes.iterator();
while (shape.hasNext()) {
shape.next();
if (shape.contains(me.getPoint())) { // <- This causes error
if (isDuck) {
score++;
} else {
score--;
shape.remove();
}
isDuck = !isDuck;
}
}
}
});
}
应该是if (shape.next().contains
。 Iterator 对象没有 contains 方法,shape.next()
返回的 Shape
对象有它。在这种情况下,您应该删除 shape.next();
行,因为您将移动到 if
内的下一个对象或相应地修改整个代码。
在 Java 中使用 Netbeans 8.2,我遇到了一个我无法理解的错误。
我正在尝试检查是否单击了 Shape 对象,然后将其从我的 Shape 对象列表中删除(因此使用了迭代器)。但是有些东西导致了一个问题,阻止 Shape.contain(Point p) 工作,给我这个错误信息:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Iterator.contains ...
这里有什么问题? contains() 不应该像这样工作吗?我错过了什么?
完整代码:
public DuckHuntPanel() {
setBackground(Color.BLACK);
shapes = new ArrayList<>();
shapes.add(ball);
Timer timer = new Timer(1000 / 60, (ActionListener) this);
timer.start();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
super.mouseClicked(me);
Iterator<Shape> shape = shapes.iterator();
while (shape.hasNext()) {
shape.next();
if (shape.contains(me.getPoint())) { // <- This causes error
if (isDuck) {
score++;
} else {
score--;
shape.remove();
}
isDuck = !isDuck;
}
}
}
});
}
应该是if (shape.next().contains
。 Iterator 对象没有 contains 方法,shape.next()
返回的 Shape
对象有它。在这种情况下,您应该删除 shape.next();
行,因为您将移动到 if
内的下一个对象或相应地修改整个代码。