2015-04-22 62 views
1

我有計時器錯誤,我不知道錯誤位於代碼中的位置。定時器執行錯誤

錯誤:

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

回答

1

你進口java.util.Timer。也許你的意思是javax.swing.Timer

你可以谷歌的更多信息,但here是一個很好的解釋,兩者之間的差異。

0

更換

import java.util.Timer; 

import javax.swing.Timer; 

好運。

0

java.util.Timer類沒有這樣的構造函數,也沒有這樣的方法。

確保您在類標題上方的導入聲明中導入正確的類。你可能意思是java.swing.Timer

如果是這樣,java.swing.Timer將不會被java.swing.*導入,因爲您已經導入了一個同名的類(java.util.Timer)。刪除java.util.Timer導入,它應該都可以工作。

來源:http://www.tutorialspoint.com/java/util/java_util_timer.htm