有人可以解释为什么我用Timer得到这个错误?

时间:2012-05-29 18:30:53

标签: java swing timer

为什么我的计时器出错了?在代码下方,它说明错误是什么。我无法弄清楚我做错了什么......任何人都可以帮助我

       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)

3 个答案:

答案 0 :(得分:6)

您应该导入javax.swing.Timer而不是java.util.Timer

答案 1 :(得分:3)

您导入了java.util.Timer。使用javax.swing.Timer。 不是你的错,你看得很多。

对于文档,请阅读http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html

答案 2 :(得分:-2)

以下行中的'this'变量:

time = new Timer(5,this);

指的是当前类,Timer类不知道如何处理;)