简体   繁体   中英

can someone explain why im getting this error with Timer?

Why am i getting an error with the timer? below the code it states what the error is. I cant figure out what im doing wrong.... can anyone help me

       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();
         }

Basically the error im getting is

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)

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

You imported java.util.Timer . Use javax.swing.Timer . Not your mistake, you see it a lot.

For the docs, read http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html .

the 'this' variable in the following line:

time = new Timer(5,this);

refers to the current class, and the Timer Class doesn't know what to do with that ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM