我一直在寻找其他一些线程以及不同的方法来解决这个问题,我一直都会遇到同样的错误。我遗漏了包裹并进口了。我正在使用Thread,但我知道你不应该将它们用于GUI。
public class Game extends JPanel{
Player player = new Player(this);
public Game(){
addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e){
}
@Override
public void keyReleased(KeyEvent e){
player.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e){
player.keyPressed(e);
}
});
}
public void move(){
player.move();
}
@Override
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
player.paint(g2d);
}
public void timer1(){
int delay = 10; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Running");
}
};
new Timer(delay, taskPerformer).start();;
Thread.sleep(10);
}
public static void main(String args[]) throws InterruptedException, Exception{
int Width = 800;
int Height = 400;
Game game = new Game();
JFrame frame = new JFrame("quest Kings");
frame.setSize(Width, Height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.green);
frame.add(game);
frame.setResizable(false);
//What to do after the program starts
while(true){
game.move();
game.repaint();
game.timer1();
}
}
}
答案 0 :(得分:5)
您导入了错误的定时器或未导入任何定时器。确保您的import语句包含
import javax.swing.Timer;
请注意,java.util.Timer
导入不计算在内。这就是为什么将您的导入包含在您的问题中至关重要。