简体   繁体   English

公共类扩展了JPanel

[英]public class extends JPanel

I am getting a error that looks like this, 我收到一个看起来像这样的错误,

run: 跑:

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)

I can't figure out what the problem with the public class is. 我不知道公共课有什么问题。

There are 2 separate files below. 下面有2个单独的文件。 Game.java Panel.java Game.java Panel.java

First File: 第一个文件:

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);
    }
}

Next File: 下一个文件:

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) {
    }
}

In java methods starts with lower case keyTyped keyReleased and keyPressed so you are not overriding KeyListener methods. 在java中,方法以小写的keyTyped keyReleasedkeyPressed开头,因此您不会覆盖KeyListener方法。

You can annotate a method with @Override this causes a compile error if it doesn't actually override. 您可以使用@Override注释方法,如果它实际上没有被覆盖,则会导致编译错误。 Section 9.6.1.4 of the JLS says: JLS的9.6.1.4节规定

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. 如果使用声明@Override注释方法声明,但该方法实际上并未覆盖超类中声明的任何方法,则将发生编译时错误。

Your class definition will lead to possible potential bugs cause 您的班级定义将导致可能的潜在错误

public class Panel extends JPanel implements Runnable, KeyListener

Calling Panel it's confusing cause already exists java.awt.Panel so call it different. 调用Panel的原因很混乱,因为java.awt.Panel已经存在,因此将其称为不同。 Implementing multiple interface like that brokes Single Responsability Principle . 像这样实现多个接口违反了单一职责原则 A possible solution is to make inner classes or anonymous classes. 一种可能的解决方案是制作内部类或匿名类。 For sure if you don't override a method is not necessary to extends JPanel . 确保如果不重写,则不需要扩展JPanel Take care that if you use KeyListener components must be in focus and be focusable and bind it to all keys, instead you can use KeyBindings . 请注意,如果您使用KeyListener组件,则组件必须KeyListener可聚焦,并将其绑定到所有键,而可以使用KeyBindings Don't use requestFocus instead use requestFocusInWindow() if you read api it says it's discouraged. 如果您阅读表明不鼓励使用的api,请不要使用requestFocus而是使用requestFocusInWindow()

The class implements the KeyListener interface but does not provide an implementation for the keyReleased , keyPressed and keyTyped methods specified on the interface. 该类实现KeyListener接口,但不提供接口上指定的keyReleasedkeyPressedkeyTyped方法的实现。 Instead it provides implementations for: KeyReleased , KeyPressed and KeyTyped which are not properly cased. 相反,它提供以下实现的实现: KeyReleasedKeyPressedKeyTyped不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM