2017-08-11 82 views
0

在Java中使用Netbeans 8.2,遇到一個錯誤我不太明白。Netbeans 8.2 Java:試圖訪問java.awt.Shape.contains()的不可編譯的代碼

我試圖檢查一個Shape對象是否被點擊或不是,然後從我的Shape對象列表中刪除它(因此使用迭代器)。可是,我是造成防止Shape.contain(點P)的工作,給我這個錯誤消息的問題:

異常在線程「AWT-EventQueue的 - 0」了java.lang.RuntimeException:不可編譯的源代碼 - 錯誤的sym類型:java.util.Iterator.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; 
        } 
       } 
      } 
     }); 
    } 
+0

你試過嗎? https://stackoverflow.com/questions/4386076/uncompilable-source-code-runtimeexception-in-netbeans –

+0

btw不應該是'if(shape.next()。contains')'Iterator'對象沒有''''方法,由'shape.next()'返回的'Shape'對象具有它。在這種情況下,您應該刪除'shape.next();'行,因爲您將移動到'if'或相應地修改整個代碼 –

+0

@ easyjoin.net我試過了,但是我無法找到這些文件,因爲Netbeans 8.2中的位置似乎已經發生了變化。 ,我還是Netbeans的新手) – Laz0rSquid

回答

0

應該是if (shape.next().contains。 Iterator對象沒有包含方法,對象由shape.next()返回。在這種情況下,您應該刪除行shape.next();,因爲您將移動到if內的下一個對象,或相應地修改整個代碼。