当前位置: 首页 > 知识库问答 >
问题:

它不是抽象的,不重写抽象方法

洪昱
2023-03-14

为什么我从下面的代码中得到这个编译错误消息?

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

Direction.java:41:错误:DirectionBoard。DirectionListener不是抽象的,并且不会覆盖KeyListener中的抽象方法keyReleated(KeyEvent)

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个答案

万俟心思
2023-03-14

再加上@Sotirios Delimanolis的回答是正确的,你可能想看看使用键绑定,而不是使用有两个大问题的键侦听器,它侦听所有键和组件必须是可聚焦的。

另一件事(如果你仍然想使用KeyListener),你可能想扩展KeyAdapter,它提供了KeyListener方法(空方法)的默认实现,因此你不需要覆盖你不使用的方法,比如键释放(keyreased)或键类型(keyTyped)。

例子:

    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();
        }
    }
袁炳
2023-03-14

meesage的错误是

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

因此,很明显,您的类并没有覆盖它需要的所有方法,例如keyReleated。如果您查看您的类,您有KeyReleated并且它是私有的

重命名这些

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

由于Java不允许子类型降低重写方法的可见性,所以要将键类型化和释放键(注意小写的k)。然后用覆盖注释每个。

如果方法实际上没有重写,则用@Override注释方法会导致编译错误。JLS第9.6.1.4节规定:

注解类型覆盖支持早期检测此类问题。如果一个方法声明用注解@覆盖进行了注解,但该方法实际上并没有覆盖超类中声明的任何方法,则会发生编译时错误。

 类似资料: