简体   繁体   English

Main.GamePanel不是抽象的,并且不会覆盖java.awt.event.KeyListener中的抽象方法keyReleased(java.awt.event.KeyEvent)

[英]Main.GamePanel is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener

I was doing a tutorial online because I wanted to make a 2d side scroller, and I got this exact error. 我当时在网上做教程,因为我想制作一个2D侧面滚动器,但出现了这个确切的错误。 I have googled it but came up with nothing. 我已经用谷歌搜索了,但是什么也没想到。 I tried looking for a typo and it looks clean, its not giving me an error anywere else in the code. 我试图寻找一个拼写错误,它看起来很干净,代码中没有其他错误。 I do not know where to start. 我不知道从哪里开始。 If you could explaing to me what the error is and how i fix it then that would be amazing. 如果您能向我解释错误是什么以及我如何解决它,那就太好了。

    package Main;


import GameState.GameStateManager;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class GamePanel extends JPanel implements Runnable, KeyListener{
    public static final int WIDTH = 320;
    public static final int HIGHT = 240;
    public static final int SCALE = 2;

    //game thread

    private Thread thread;
    private boolean running;
    private int FPS = 60;
    private long targetTime = 1000/FPS;
    //image        
    private BufferedImage image;
    private Graphics2D g;

    //game state manager
    private GameStateManager gsm;




    public GamePanel(){
        super();
        setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        setFocusable(true);
        requestFocus();
    }
    public void addNotify(){
        super.addNotify();
        if (thread == null) {
            thread = new Thread(this);
            addKeyListener(this);
            thread.start();
        }
    }   
    private void init() {
        image = new BufferedImage(WIDTH, HIGHT, BufferedImage.TYPE_INT_RGB);     
        g = (Graphics2D) image.getGraphics();
        running = true;
        gsm = new GameStateManager();

    }

        public void run(){
        init();
        long start, elapsed, wait;

        //game loop
          while(running) {

            start = System.nanoTime();
            update();
            draw();
            drawToScreen();
               elapsed = System.nanoTime() - start;

               wait = targetTime - elapsed / 1000000;

               try
               {
                   Thread.sleep(wait);
               }
               catch(Exception e) 
               {
                   e.printStackTrace();
               }//end of try catch
          }







        }
        private void update()
        {
            gsm.update();
        }
        private void draw()
        {
            gsm.draw(g);
        }
        private void drawToScreen()
        {
            Graphics g2 = getGraphics();
            g2.drawImage(image, 0, 0, null);
            g2.dispose();


        }




        public void KeyPressed(KeyEvent key) 
        {
         gsm.keyPressed(key.getKeyCode());
        }
        public void KeyReleased(KeyEvent key) 
        {
         gsm.keyReleased(key.getKeyCode());
        }




    }

The compiler error message tells you exactly what's wrong: your class implements the KeyListener interface but does not implement all the necessary methods of the interface. 编译器错误消息告诉您确切的问题所在:您的类实现了KeyListener接口,但没有实现该接口的所有必需方法。 Solution: be sure to implement all the necessary methods as per the KeyListener API. 解决方案:确保按照KeyListener API实施所有必需的方法。 Also be sure to use the @Override annotation to make sure that your overrides are correct. 另外,请确保使用@Override批注以确保您的替代正确。

Having said that, I'm going to recommend that you not use KeyListeners for most key board input with Swing applications, that it is a low-level listener and should be avoided in favor of higher level constructs such as key bindings . 话虽如此,我建议您不要在Swing应用程序的大多数键盘输入中使用KeyListeners,因为它是低级侦听器,应避免使用高级结构,例如键绑定 Also, Swing GUI's should avoid use of update(...) method overrides as that is more of an AWT construct. 另外,Swing GUI应该避免使用update(...)方法重写,因为它更多地是AWT构造。

Your KeyReleased(KeyEvent key) method must start with small letter 'k' like keyReleased(KeyEvent key) . 您的KeyReleased(KeyEvent key)方法必须以小写字母'k'开头,例如keyReleased(KeyEvent key) Java is case sensitive. Java区分大小写。

You may also required to override other methods of KeyListener interface. 您可能还需要重写KeyListener接口的其他方法。

Also add @Override annotation (as suggested by @Hovercraft Full Of Eels) to the method when you want to override a super method. 当您要覆盖超级方法时,还应在方法中添加@Override注释(如@Hovercraft Full Of Eels所建议)。 That way IDE's will give you hint's while coding. 这样,IDE会在编码时为您提供提示。

暂无
暂无

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

相关问题 java:按钮不是抽象的,并且不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent) - java: Button is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener 类不是抽象的,并且不会重写KeyListener中的抽象方法keyReleased(KeyEvent) - Class is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener 错误:WindowText不是抽象的,并且不会覆盖java.awt.event.MouseListener中的抽象方法mouseExited(java.awt.event.MouseEvent) - Error: WindowText is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener java.awt.event.KeyEvent无法完全映射pin AZERTY键盘吗? - java.awt.event.KeyEvent not capable of fully mappin AZERTY keyboard? 私有void formKeyPressed(java.awt.event.KeyEvent evt) - private void formKeyPressed(java.awt.event.KeyEvent evt) 加速键值java.awt.event.KeyEvent - accelerator key value java.awt.event.KeyEvent “必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)”是什么意思? - what does “must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)” mean? 即使未使用keyReleased,“ &lt;&gt;也不是抽象的,并且不会覆盖抽象方法keyReleased” - '<> is not abstract and does not override abstract method keyReleased' even though keyReleased is not used 类不是抽象的,并且不覆盖抽象方法AWT程序 - Class is not abstract and does not override abstract method AWT Program Java 7但不是Java 6:“不是抽象的,不会覆盖抽象方法” - Java 7 but not Java 6: “is not abstract and does not override abstract method”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM