有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java公共类扩展了JPanel

我得到了一个类似这样的错误

运行:

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)

我搞不懂公共课有什么问题

下面有两个单独的文件。 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) {
    }
}

共 (2) 个答案

  1. # 1 楼答案

    该类实现了KeyListener接口,但没有为接口上指定的keyReleasedkeyPressedkeyTyped方法提供实现。相反,它为:KeyReleasedKeyPressedKeyTyped提供了未正确区分大小写的实现

  2. # 2 楼答案

    在java中,方法以小写的keyTypedkeyReleasedkeyPressed开头,因此您没有覆盖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。所以称之为不同。实现这样的多个接口会中断Single Responsability Principle。一种可能的解决方案是创建内部类或匿名类。当然,如果不重写,扩展JPanel就不需要方法。注意,如果使用KeyListener组件必须处于焦点且可聚焦,并将其绑定到所有键,相反,可以使用KeyBindings。不要使用requestFocus而是使用requestFocusInWindow()如果你读了api,它会说不鼓励使用