java - 定时器执行错误

标签 java timer

我的计时器出现错误,并且我不知道错误在代码中的位置。

错误:

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/

相关文章:

java - 如何在 Mac 上使用 Java 中的 ssl 证书

java - AWS Elastic Beanstalk "Impaired services on all instances."

java - 将 Swing 计时器的操作监听器中的 System.currentTimeMillis() 分配回未初始化的 long 变量

android - 如何在一定时间后开始 Activity ?

java - Java 中的隐式转换

Java除法循环

java - Android Studio - 使用 jsoup 解析抛出错误 (java.lang.NullPointerException)

Android:如何在指定时间开始一个 Activity ?

ios - 多个计时器一次 ios

java - 慢慢地画一条线