它不是抽象的,不会覆盖抽象方法

时间:2013-12-28 20:55:34

标签: java swing keylistener

为什么我从下面的代码中收到此编译错误消息?

(程序根据按下的箭头键在4个方向上移动箭头  在键盘上:d)

  

Direction.java:41:错误:DirectionPanel.DirectionListener不是   抽象并且不重写抽象方法keyReleased(KeyEvent)   在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);
      }
    }

2 个答案:

答案 0 :(得分:10)

错误消息是

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

很明显,你的类不会覆盖它需要的所有方法,例如keyReleased。如果你看看你的课程,你有KeyReleased而且private

重命名这些

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

keyTypedkeyReleased(请注意小写k)并将它们设为public,因为Java不允许子类型降低覆盖方法的可见性。然后用@Override注释每个。

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

  

注释类型覆盖支持早期检测此类问题。如果使用注释@Override注释方法声明,但该方法实际上不会覆盖超类中声明的任何方法,则会发生编译时错误。

答案 1 :(得分:2)

添加到@Sotirios Delimanolis的答案是正确的,你可能想看看使用Keybindings而不是使用有两大问题的keyListener,它会监听所有键和组件必须是可聚焦的并且是焦点。

另一件事(如果你仍然想使用keyListeners)你可能想扩展KeyAdapter提供KeyListener方法的默认实现(空方法),因此你不需要覆盖你不使用的方法例如keyReleasedkeyTyped

示例:

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