-->

It's not abstract does not override abstract m

2020-05-06 14:37发布

问题:

Why am I getting this compile error message from the code below?

(The program moves an arrow round in 4 directions depending on which arrow key is pressed on the keyboard:d)

Direction.java:41: error: DirectionPanel.DirectionListener is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener

private class DirectionListener implements KeyListener  {

    //Direction.java

    import javax.swing.*;
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;

    class DirectionPanel extends JPanel  {

      private final int WIDTH = 300, HEIGHT = 200;
      private final int JUMP = 10; //increment for image movement
      private final int IMAGE_SIZE = 31;
      private ImageIcon up, down, right, left, currentImage;
      private int x, y;

      //constructor:
      public DirectionPanel ()  {

      addKeyListener (new DirectionListener());
      x =WIDTH / 2;
      y = HEIGHT / 2;
      up = new ImageIcon ("arrowUp.gif");
      down = new ImageIcon ("arrowDown.gif");
      left = new ImageIcon ("arrowLeft.gif");
      right = new ImageIcon ("arrowRight.gif");

      currentImage = right;

      setBackground (Color.black);
      setPreferredSize (new Dimension(WIDTH, HEIGHT));
      setFocusable(true);
      }

      //draws
      public void paintComponent (Graphics page)  {
        super.paintComponent (page);
        currentImage.paintIcon (this, page, x, y);
      }

      //represents the listener for keyboard activity
      private class DirectionListener implements KeyListener  {
        public void keyPressed (KeyEvent event)  {
          switch (event.getKeyCode())  {
            case KeyEvent.VK_UP:
                currentImage = up;
                y -= JUMP;
                break;
            case KeyEvent.VK_DOWN:
                currentImage = down;
                y += JUMP;
                break;
            case KeyEvent.VK_LEFT:
                currentImage = left;
                x -= JUMP;
                break;
            case KeyEvent.VK_RIGHT:
                currentImage = right;
                x += JUMP;
                break;
          }
        repaint();
        }
      //empty definitions for unused methods
      private void KeyTyped (KeyEvent event) {}
      private void KeyReleased (KeyEvent event) {}

      }
    }


    public class Direction  {

      public static void main (String[] args)  {

      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add (new DirectionPanel());
      frame.pack();
      frame.setVisible(true);
      }
    }

回答1:

The error meesage is

DirectionPanel.DirectionListener is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener private class DirectionListener implements KeyListener

So obviously your class does not override all the methods it needs to, for example keyReleased. If you look at your class, you have KeyReleased and it's private.

Rename these

private void KeyTyped (KeyEvent event) {}
private void KeyReleased (KeyEvent event) {}

to keyTyped and keyReleased (note the lowercase k) and make them public since Java doesn't allow sub types to reduce the visibility of an overriden method. Then annotate each with @Override.

Annotating a method with @Override 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.



回答2:

Adding to @Sotirios Delimanolis answer that is correct, you may want to take a look to use Keybindings instead of using keyListeners that has 2 big issues, it listens for all keys and components must be focusable and in focus.

Another thing (if you still want to use keyListeners) you may want to extends KeyAdapter that provide default implementation of KeyListener methods (empty methods) therefore you don't need to override methods that you don't use like keyReleased or keyTyped.

Example:

    private class DirectionListener extends KeyAdapter  {
       @Override 
       public void keyPressed (KeyEvent event)  {
          switch (event.getKeyCode())  {
            case KeyEvent.VK_UP:
                currentImage = up;
                y -= JUMP;
                break;
            case KeyEvent.VK_DOWN:
                currentImage = down;
                y += JUMP;
                break;
            case KeyEvent.VK_LEFT:
                currentImage = left;
                x -= JUMP;
                break;
            case KeyEvent.VK_RIGHT:
                currentImage = right;
                x += JUMP;
                break;
          }
        repaint();
        }
    }