java - 公共(public)类扩展了 JPanel

标签 java class compiler-errors jpanel extends

我收到如下错误,

运行:

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)

我不明白公共(public)类的问题是什么。

下面有 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) {
    }
}

最佳答案

在 java 方法中,以小写的 keyTyped keyReleasedkeyPressed 开头,因此您不会重写 KeyListener 方法。

您可以使用@Override注释方法 如果它实际上没有覆盖,这会导致编译错误。 Section 9.6.1.4 of the JLS说:

The annotation type Override supports early detection of such problems. If a method declaration is annotated with the annotation @Override, but the method does not in fact override any method declared in a superclass, a compile-time error will occur.

您的类定义将导致可能的潜在错误原因

public class Panel extends JPanel implements Runnable, KeyListener

调用Panel很令人困惑,因为已经存在java.awt.Panel,所以用不同的方式调用它。实现像这样的多个接口(interface)会破坏Single Responsability Principle 。一个可能的解决方案是创建内部类或匿名类。当然,如果您不重写方法,则无需扩展 JPanel。请注意,如果您使用 KeyListener 组件必须处于焦点并且可聚焦并将其绑定(bind)到所有键,您可以使用 KeyBindings 。如果您阅读了 api,它表示不鼓励使用,请不要使用 requestFocus 而应使用 requestFocusInWindow()

关于java - 公共(public)类扩展了 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20862919/

相关文章:

C# 应用程序无法从 VB 类库中找到类型

C++ 错误 : no matching function call from pointer to pointer reference using Qt and QVariant

c++ - 在C++中多次使用逻辑运算符 '&&'时发生代码错误

java - 在动态网页上从数据库中搜索数据

java - 逐字符比较java中的两个字符串

php - 如何从 lambda 函数访问父对象?

java - 从另一个类添加 Swing 组件

python - 为什么下面的代码会产生缩进错误?

java - 如何将第 3 方库添加到 Android AOSP 构建中?

java - Libgdx |场景2d |如何在动画上使用 Action