我的计时器出现错误,并且我不知道错误在代码中的位置。
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor Timer(int, Player) is undefined The method start() is undefined for the type Timer
at Player.(Player.java:12)
at Game.main(Game.java:11)
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.Timer;
import javax.swing.*;
public class Player extends JPanel implements ActionListener{
Timer t = new Timer(5, this); // Error (LINE 12)
double x = 0; double velX = 2;
double y = 0; double velY = 2;
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x,y,40,40);
g2.fill(circle);
t.start(); // error
}
public void actionPerformed(ActionEvent e){
x += velX;
y += velY;
repaint();
}
}
最佳答案
您已导入java.util.Timer
。也许您的意思是javax.swing.Timer
?
您可以通过谷歌搜索了解更多信息,但是here很好地解释了两者之间的差异。
关于java - 定时器执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29803780/