2012-05-29 38 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

相关问题