我收到的错误看起来像这样,
运行:
Exception in thread "main" java.lang.ExceptionInInitializerError
at ao.Game.main(Game.java:11)
Caused by: java.lang.RuntimeException: Uncompilable source code -
ao.Panel is not abstract and does not override abstract method
keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener
at ao.Panel.<clinit>(Panel.java:15)
... 1 more
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
我无法弄清楚公共类的问题是什么。
下面有2个单独的文件。 Game.java Panel.java
第一档:
package ao;
import ao.Panel;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame("2D Shooter Pre-Alpha");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Panel());
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
下一个文件:
package ao;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/**
*
* @author andyoppenheimer
*/
public class Panel extends JPanel implements Runnable, KeyListener {
// panel dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
// main loop
private Thread thread;
private boolean running = false;
private int fps = 60;
private long targetTime = 1000 / fps;
// drawing
private Graphics2D g;
private BufferedImage image;
public Panel() {
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setFocusable(true);
requestFocus();
}
public void addNotify() {
super.addNotify();
if (thread == null) {
running = true;
addKeyListener(this);
thread = new Thread(this);
thread.start();
}
}
public void init() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
}
public void update() {
}
public void draw() {
g.clearRect(0, 0, WIDTH, HEIGHT);
}
public void drawToScreen() {
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
g2.dispose();
}
public void run() {
init();
long start;
long elapsed;
long wait;
while (running == true) {
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if (wait < 0) {
wait = 5;
}
try {
Thread.sleep(wait);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void KeyPressed(KeyEvent k) {
}
public void KeyReleased(KeyEvent k) {
}
public void KeyTyped(KeyEvent k) {
}
}
答案 0 :(得分:2)
在java方法中,从小写keyTyped
keyReleased
和keyPressed
开始,因此您不会覆盖KeyListener
方法。
您可以使用@Override注释方法 如果它实际上没有覆盖,这会导致编译错误。 Section 9.6.1.4 of the JLS说:
注释类型覆盖支持早期检测此类问题。如果使用注释@Override注释方法声明,但该方法实际上不会覆盖超类中声明的任何方法,则会发生编译时错误。
您的课程定义会导致潜在错误导致
public class Panel extends JPanel implements Runnable, KeyListener
调用Panel
这是令人困惑的原因已经存在java.awt.Panel所以称之为不同。实现多个类似于Single Responsability Principle的界面。一种可能的解决方案是创建内部类或匿名类。当然,如果不覆盖方法,则无需扩展JPanel
。请注意,如果您使用KeyListener
组件必须处于焦点并且可以聚焦并将其绑定到所有键,您可以使用KeyBindings。不要使用requestFocus
代替使用requestFocusInWindow()
,如果你读了api,它说不鼓励。
答案 1 :(得分:1)
该类实现KeyListener接口,但不提供接口上指定的keyReleased
,keyPressed
和keyTyped
方法的实现。相反,它提供了以下内容的实现:KeyReleased
,KeyPressed
和KeyTyped
未正确套装。