英文:
The type SnakeGame must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)
问题
我正在尝试创建一个类似贪吃蛇的游戏,尽管我已经尽力“实现”了 KeyListener,但我在 SnakeGame 类中得到了错误消息 - "SnakeGame 类必须实现继承的抽象方法 KeyListener.keyReleased(KeyEvent)"。
我是 Java 新手,无法想出可能的解决方法,我已经寻找了关于如何正确实现 KeyListener 的教程,并尝试了我能想到的一切。
感谢任何形式的帮助。
以下是我的代码-
import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class SnakeGame implements KeyListener {
public JFrame board;
public static void main(String[] args) {
SnakeGame sm = new SnakeGame();
sm.initGUI();
}
public void initGUI() {
JFrame board = new JFrame("Snake");
board.addKeyListener(this);
board.setFocusable(true);
board.setFocusTraversalKeysEnabled(false);
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.setLocationRelativeTo(null);
board.setVisible(true);
board.pack();
board.setSize(300, 300);
}
KeyListener listener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
System.out.println("你按下了上键");
break;
case KeyEvent.VK_LEFT:
System.out.println("你按下了左键");
break;
case KeyEvent.VK_RIGHT:
System.out.println("你按下了右键");
break;
case KeyEvent.VK_DOWN:
System.out.println("你按下了下键");
}
}
};
}
希望这可以帮助你解决问题。
英文:
i am trying to create a snake type game and despite my best efforts at "implementing" KeyListener, i get the error message at class SnakeGame- "The type SnakeGame must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)".
I am new to java and cannot think of a possible way to fix this, i've looked for tutorials on how to implement KeyListener properly, and tried everything i can think of.
Any kind of help is appreciated.
Here is my code-
import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class SnakeGame implements KeyListener {
public JFrame board;
public static void main(String[] args) {
SnakeGame sm = new SnakeGame();
sm.initGUI();
}
public void initGUI() {
JFrame board = new JFrame("Snake");
board.addKeyListener(this);
board.setFocusable(true);
board.setFocusTraversalKeysEnabled(false);
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.setLocationRelativeTo(null);
board.setVisible(true);
board.pack();
board.setSize(300, 300);
}
KeyListener listener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
System.out.println("you pressed up");
break;
case KeyEvent.VK_LEFT:
System.out.println("you pressed left");
break;
case KeyEvent.VK_RIGHT:
System.out.println("you pressed right");
break;
case KeyEvent.VK_DOWN:
System.out.println("you pressed down");
}
}
};
}
答案1
得分: 0
你应该能够将鼠标悬停在错误上(它应该会显示在类定义 'SnakeGame' 上的错误),然后按下“添加未实现的方法”或者简单地在你的类主体中定义那个方法。
英文:
You should be able to hover over the error(it should be showing you the error on the class def 'SnakeGame') and then press 'Add unimplemented methods' or just simply define that method in your class body.
答案2
得分: 0
如果您想在您的类中实现KeyListener
,那么您需要在您的类中实现KeyListener
接口的所有方法。这意味着您需要将keyPressed
、keyTyped
和keyReleased
作为方法实现在您的类中,您尚未这样做。
相反地,您正试图使用KeyAdapter
并覆盖适配器的单个方法,但您这样做是不正确的。
所以要修复您的问题,不要实现KeyListener
。您的类定义应该是:
//public class SnakeGame implements KeyListener {
public class SnakeGame {
然后现在您可以如下所示使用KeyAdapter
:
public void initGUI() {
KeyListener listener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
System.out.println("you pressed up");
break;
case KeyEvent.VK_LEFT:
System.out.println("you pressed left");
break;
case KeyEvent.VK_RIGHT:
System.out.println("you pressed right");
break;
case KeyEvent.VK_DOWN:
System.out.println("you pressed down");
}
}
};
JFrame board = new JFrame("Snake");
//board.addKeyListener(this);
board.addKeyListener(listener);
这是一个更好的方法,比起让您的类实现KeyListener
,因为您可以为每个组件使用不同的KeyAdapter
。
然而,如果您真的想知道如何在类中实现KeyListener
,请阅读Swing教程中关于如何编写KeyListener部分,其中有一个可工作的示例。
将Swing教程的链接保留在手边,以备查阅Swing基础知识。
另外,请注意,您不应该使用KeyListener
来监听游戏的按键事件。相反,您应该使用Key Bindings
。教程中还有一个关于How to Use Key Bindings
的部分。
英文:
If you want to implement KeyListener
in your class then you need to implement all the method of the KeyListener interface in your class. Which means you need to implement keyPressed, keyTyped and keyReleased as methods in your class you have not done that.
Instead you are trying to use a KeyAdapter
and override a single method of the adapter, but you are doing this incorrectly.
So to fix your problem, don't implement KeyListener. Your class definition should be:
//public class SnakeGame implements KeyListener {
public class SnakeGame {
And then now you can use the KeyAdapter as follows:
public void initGUI() {
KeyListener listener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
System.out.println("you pressed up");
break;
case KeyEvent.VK_LEFT:
System.out.println("you pressed left");
break;
case KeyEvent.VK_RIGHT:
System.out.println("you pressed right");
break;
case KeyEvent.VK_DOWN:
System.out.println("you pressed down");
}
}
};
JFrame board = new JFrame("Snake");
//board.addKeyListener(this);
board.addKeyListener(listener);
This is a better approach they trying to have you class implement KeyListener, because you can have a different KeyAdapter for each component.
However, if you really want to know how to implement a KeyListener in a class then read the section from the Swing tutorial on How to Write a KeyListener for a working example.
Keep a link to the Swing tutorial handy for all Swing basics.
Also, note you should not be using a KeyListener to listen for key events for you game. Instead you should be using Key Bindings
. The tutorial also has a section on How to Use Key Bindings
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论