1
嘿家伙我想用java中的keylistener移动一个带箭头键的矩形。我得到了一切工作并导入了所有内容,但它说类名不是在非抽象类“SoccerGame”中实现的。我不知道该怎么办。我试图做出很多改变以使其起作用,但它仍然不起作用。还有更多我需要导入的软件包吗?希望你们能帮忙。谢谢。用keylistener /箭头键移动图形
// The "SoccerGame" class.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class SoccerGame extends Applet implements KeyListener
{
int x = 200, y = 200;
public void init()
{
this.requestFocus();
addKeyListener (this);
setSize (800, 550);
} // init method
public void paint (Graphics g)
{
g.drawRect (20, 20, 340, 340);
g.fillRect (x, y, 20, 20);
} // paint method
public void keyPressed (KeyEvent e)
{
if (e.getKeyCode()==e.VK_UP)
{
y = y-10;
}
if (e.getKeyCode()==e.VK_DOWN)
{
y = y+10;
}
if (e.getKeyCode()==e.VK_LEFT)
{
x = x-10;
}
if (e.getKeyCode()==e.VK_RIGHT)
{
x = x+10;
}
repaint();
}
public void keyReleased (KeyEvent e)
{
}
} // SoccerGame class