2012-05-29 62 views
1

爲什麼我在計時器中出錯?在代碼下面說明錯誤是什麼。我無法弄清楚什麼即時做錯了....誰能幫我有人可以解釋爲什麼我得到這個錯誤與計時器?

 import java.util.Timer; 
     import javax.swing.ImageIcon; 
     import javax.swing.JPanel; 

     /** 
     * 
     * @author Rich 
     */ 
    public class Board extends JPanel implements ActionListener{ 
     Dude p; 
     Image img; 
     Timer time; 

     public Board() { 
     p = new Dude(); 
     addKeyListener(new AL()); 
     setFocusable(true); 
      ImageIcon i = new ImageIcon("images.jpg"); 
      img = i.getImage(); 
      time = new Timer(5,this); 
      time.start(); 
     } 

     public void actionPerformed(ActionEvent e) { 
     p.move(); 
     repaint(); 
     } 

基本上錯誤即時得到是

no suitable constructor found for Timer(int,OurGame.Board) 
constructor java.util.Timer.Timer(java.lang.String,boolean) is not applicable 
    (actual argument int cannot be converted to java.lang.String by method invocation conversion) 
constructor java.util.Timer.Timer(java.lang.String) is not applicable 
    (actual and formal argument lists differ in length) 
constructor java.util.Timer.Timer(boolean) is not applicable 
    (actual and formal argument lists differ in length) 
constructor java.util.Timer.Timer() is not applicable 
    (actual and formal argument lists differ in length) 
+0

閱讀文檔。 – asawyer

+0

正如錯誤狀態一樣;沒有'Timer' ctor需要'Board'參數。 –

+0

是的,我使用了錯誤的定時器 –

回答

6

您應該導入javax.swing.Timer而不是java.util.Timer

-2

在以下行「這個」變量:

time = new Timer(5,this); 

指的是當前類,而定時器類不知道該如何處理;)

+0

非常錯誤,他的類實現了ActionListener,所以它是一個有效的參數。先閱讀文檔,或自己嘗試。 – Hidde

相關問題