我在将 keyListener 添加到我的应用程序时遇到问题:
App.Game
不是抽象的,并且不会覆盖keyRelased(java.awt.event.KeyEvent)
java.awt.event.KeyListener 中的抽象方法
我尝试创建新的公共课程KeyAdapt
。它给出了同样的错误。
我的应用程序代码是:
public int x = 100, y = 100;
public class Game extends JFrame implements KeyListener {
public Game() {
super();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setUndecorated(true);
setLayout(null);
setVisible(true);
}
public void paint (Graphics gDC) {
gDC.clearRect(0, 0, getSize().width, getSize().height);
getGraphics().drawOval(x, y, 20, 20);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP ) {
y++;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN ) {
y--;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT ) {
x++;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT ) {
x--;
}
}
public void keyRelased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP ) {
y+=0;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN ) {
y-=0;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT ) {
x+=0;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT ) {
x-=0;
}
}
public void keyTyped(KeyEvent e) {}
}