这里的新Java用户有一个可能很愚蠢的问题。请耐心等待......
这是我的代码:
package javagame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class JavaGame extends JFrame {
int x, y;
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
x--;
}
if (keyCode == e.VK_RIGHT) {
x++;
}
if (keyCode == e.VK_UP) {
y--;
}
if (keyCode == e.VK_DOWN) {
y++;
}
}
public void keyReleased(KeyEvent e) {
}
}
public JavaGame() {
addKeyListener(newAL());
setTitle("Java Game");
setSize(250, 250);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 150;
y = 150;
}
public void paint(Graphics g) {
g.fillOval(x, y, 15, 15);
repaint();
}
public static void main(String[] args) {
new JavaGame();
}
}
现在,当我尝试运行它时,构建成功但仍然会弹出错误:
cannot find symbol
symbol: method newAL()
location class JavaGame
任何想法?
答案 0 :(得分:3)
您正在尝试调用您没有的函数newAL
。使用:
new AL()
new关键字指定您要创建AL类的新实例。