Java 类不是抽象的,不会覆盖 KeyListener 中的抽象方法 keyReleased(KeyEvent)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21059972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener
提问by user3184504
I am trying to make a small game where you have to dodge rectangles as an oval and I encountered this problem.
我正在尝试制作一个小游戏,您必须将矩形躲避为椭圆形,但我遇到了这个问题。
Dodge is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener
I have scoured the internet to try to find an answer but I cannot find a fix to it.
我在互联网上搜索试图找到答案,但我找不到解决方法。
package dodge;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.Graphics;
public class Dodge extends JPanel implements KeyListener {
private int x = 5, y = 5;
public Dodge(){
setSize(new Dimension(500, 400));
setPreferredSize(new Dimension(500, 400));
//setBackground(Color.BLACK);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.fillOval(x, y, 20, 20);
repaint();
}
public static void main(String[] args) {
Dodge game = new Dodge();
JFrame frame = new JFrame();
frame.setTitle("Dodge the Rectangles");
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setSize(new Dimension(500, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
回答by Saurabh Sharma
You have to Override a method keyReleased(KeyEvent)
as you are implementing an interface (KeyListener
).
您必须keyReleased(KeyEvent)
在实现接口 ( KeyListener
) 时覆盖方法。
Rule:You have to override all the methods defined in an interface that you are implementing if your class is not abstract.
规则:如果您的类不是抽象类,则必须覆盖您正在实现的接口中定义的所有方法。
Add this code in your Dodge
class and give a try.
将此代码添加到您的Dodge
课程中并尝试一下。
public void keyReleased(KeyEvent e)
{
//code.
}
As KeyListener
has two more methods.
由于KeyListener
有两个方法。
keyTyped(KeyEvent e)
keyPressed(KeyEvent e)
keyTyped(KeyEvent e)
keyPressed(KeyEvent e)
You need to override them as well if you want your class to keep implementing KeyListener
.
如果您希望您的类继续实现KeyListener
.
回答by Robin Green
You have added implements KeyListener
, which means "I am going to implement the methods defined in the KeyListener interface", but you haven't actually done so.
您添加了implements KeyListener
,这意味着“我将实现 KeyListener 接口中定义的方法”,但您实际上并没有这样做。
If you want your game to be controllable by the keyboard, you can still use KeyListener (although it's kind of an old technique). But if you do, you will have to implement its methods.
如果您希望您的游戏可以通过键盘进行控制,您仍然可以使用 KeyListener(尽管它是一种古老的技术)。但是如果你这样做了,你将不得不实现它的方法。
If you don't, and this was a mistake, just remove the implements KeyListener
.
如果你不这样做,这是一个错误,只需删除implements KeyListener
.
回答by Patricia Shanahan
As already pointed out, you need to write or inherit every method that is required by any interface you implement. As a convenience implementing interfaces, an interface may have associated with it a class that provides default implementations for some or all of its methods.
正如已经指出的,您需要编写或继承您实现的任何接口所需的每个方法。为了方便实现接口,接口可能会关联一个类,该类为其某些或所有方法提供默认实现。
In the case of KeyListener, the convenience class is java.awt.event.KeyAdapter. It provides empty methods. If you extend it, you only need to directly implement the method you want.
在 KeyListener 的情况下,便利类是 java.awt.event.KeyAdapter。它提供空方法。如果你扩展它,你只需要直接实现你想要的方法。
To find the convenience class for an interface, scan its "All Known Implementing Classes" list.
要查找接口的便利类,请扫描其“所有已知实现类”列表。
回答by David Mon Flo
I had the same problem, it looks like a big problem but it isn't. Just add the following line to your code
我遇到了同样的问题,看起来是个大问题,但事实并非如此。只需将以下行添加到您的代码中
@Override public void keyReleased( KeyEvent e ){}