【发布时间】:2013-12-31 20:34:29
【问题描述】:
我收到一个类似这样的错误,
运行:
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) {
}
}
【问题讨论】:
-
g = (Graphics2D) image.getGraphics();啊啊啊!无论您使用什么资源来学习 Java,都将其丢失。这是垃圾。而是转到Performing Custom Painting.. 对于 Swing,通常在基于 AWT 的较低级别KeyListener上使用键绑定。有关如何使用它们的详细信息,请参阅How to Use Key Bindings。此外,尝试实际阅读该组。这类事情一天会出现 2-3 次! -
@AndrewThompson 您能否评论一下更好的方法以及为什么那行代码让您如此厌恶?
-
@KevinBowersox 查看编辑。
-
@AndrewThompson 您可以发表评论作为答案:)
-
@nachokk 不幸的是,它甚至没有开始解决编译问题。我认为这已经处理得很好了。
标签: java class compiler-errors jpanel extends