简体   繁体   中英

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. 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.

You can annotate a method with @Override this causes a compile error if it doesn't actually override. Section 9.6.1.4 of the JLS says:

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.

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. 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 . 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 . Don't use requestFocus instead use requestFocusInWindow() if you read api it says it's discouraged.

The class implements the KeyListener interface but does not provide an implementation for the keyReleased , keyPressed and keyTyped methods specified on the interface. Instead it provides implementations for: KeyReleased , KeyPressed and KeyTyped which are not properly cased.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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