哪个更好:Key Listener 还是 key Adapter?
What is better: Key Listener or key Adapter?
我正在编写一个游戏,玩家可以使用 WASD 键四处移动。我知道有两种方法可以为这种情况注册键盘输入:使用按键监听器或按键适配器。
然而,每个人的好处还不是很清楚,Java 的创作者没有理由让两个 类 做同样的事情。
我想知道哪个是更好的选择。
public abstract class KeyAdapter extends Object implements KeyListener
An abstract adapter class for receiving keyboard events. The methods in this class are empty. This class exists as convenience for creating listener objects.
实际上似乎是 KeyAdapter is a class that implements KeyListener 接口,因此我们无法讨论哪个更好,因为实现和接口不相等
您实际上可以做的只是扩展 KeyAdapter class 并根据您的实际需要添加一些对键盘事件的额外支持
Here 您可以找到有关如何处理此主题的示例
- KeyAdapter (abstract class): 需要not实现所有的方法——只需提供所需方法的代码(s).如果应用程序扩展此 class,则将没有机会扩展任何其他 class。
- KeyListener(interface):如果要实现这个接口,需要实现这三个方法。但是,以后有可能扩展其他 classes。
I want to know which would be the better option.
这取决于您的应用程序设计。一些示例场景:
public class MyGuiApp extends KeyAdapter implements ActionListener {
// Override any -or- all of the three KeyAdapter class methods as needed
// No possibility of extending any other class
}
public class MyGuiApp implements KeyListener, ActionListener {
// Override all of the three methods of KeyListener
// Can have empty method bodies for the ones not used
// Possibility of extending any other class, later
}
public class MyGuiApp {
// ...
public void buildGui() {
// ...
JButton button = new JButton("Press me");
button.addKeyListener(new MyKeyListener());
// ...
}
// NOTE: Application will need only one of the implementations, in this sample.
// KeyAdapeter -or- KeyListener
private class MyKeyListener extends KeyAdapter {
// NOTE: Only one method is implemented - in this case
@Override public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
// do some ui related action...
}
}
}
private class MyKeyListener implements KeyListener {
// NOTE: Only all methods are reuired to be implemented - in this case
@Override public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
// do some ui related action...
}
}
@Override public void keyReleased(KeyEvent e) {
// No code - as its not used
}
@Override public void keyTyped(KeyEvent e) {
// No code - as its not used
}
}
}
示例代码links:
- 这里是 link 的示例(带有完整的 运行 代码)用法
KeyAdapter
:JButton KeyPressed - 无
发生.
- 这是 link 到 Java 使用
KeyListener
的教程:如何编写
关键
听众.
有一个小区别:
KeyListener
编译器会报错
error: <anonymous class> is not abstract and does not override abstract method
keyReleased(KeyEvent) in KeyListener
而在IDE你会得到
Add UnImplemented Methods
KeyAdapter
您不会收到任何错误消息
因此,如果你只想override
一些方法,使用KeyAdapter
很方便。
我正在编写一个游戏,玩家可以使用 WASD 键四处移动。我知道有两种方法可以为这种情况注册键盘输入:使用按键监听器或按键适配器。
然而,每个人的好处还不是很清楚,Java 的创作者没有理由让两个 类 做同样的事情。
我想知道哪个是更好的选择。
public abstract class KeyAdapter extends Object implements KeyListener
An abstract adapter class for receiving keyboard events. The methods in this class are empty. This class exists as convenience for creating listener objects.
实际上似乎是 KeyAdapter is a class that implements KeyListener 接口,因此我们无法讨论哪个更好,因为实现和接口不相等
您实际上可以做的只是扩展 KeyAdapter class 并根据您的实际需要添加一些对键盘事件的额外支持
Here 您可以找到有关如何处理此主题的示例
- KeyAdapter (abstract class): 需要not实现所有的方法——只需提供所需方法的代码(s).如果应用程序扩展此 class,则将没有机会扩展任何其他 class。
- KeyListener(interface):如果要实现这个接口,需要实现这三个方法。但是,以后有可能扩展其他 classes。
I want to know which would be the better option.
这取决于您的应用程序设计。一些示例场景:
public class MyGuiApp extends KeyAdapter implements ActionListener {
// Override any -or- all of the three KeyAdapter class methods as needed
// No possibility of extending any other class
}
public class MyGuiApp implements KeyListener, ActionListener {
// Override all of the three methods of KeyListener
// Can have empty method bodies for the ones not used
// Possibility of extending any other class, later
}
public class MyGuiApp {
// ...
public void buildGui() {
// ...
JButton button = new JButton("Press me");
button.addKeyListener(new MyKeyListener());
// ...
}
// NOTE: Application will need only one of the implementations, in this sample.
// KeyAdapeter -or- KeyListener
private class MyKeyListener extends KeyAdapter {
// NOTE: Only one method is implemented - in this case
@Override public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
// do some ui related action...
}
}
}
private class MyKeyListener implements KeyListener {
// NOTE: Only all methods are reuired to be implemented - in this case
@Override public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
// do some ui related action...
}
}
@Override public void keyReleased(KeyEvent e) {
// No code - as its not used
}
@Override public void keyTyped(KeyEvent e) {
// No code - as its not used
}
}
}
示例代码links:
- 这里是 link 的示例(带有完整的 运行 代码)用法
KeyAdapter
:JButton KeyPressed - 无 发生. - 这是 link 到 Java 使用
KeyListener
的教程:如何编写 关键 听众.
有一个小区别:
KeyListener
编译器会报错
error: <anonymous class> is not abstract and does not override abstract method
keyReleased(KeyEvent) in KeyListener
而在IDE你会得到
Add UnImplemented Methods
KeyAdapter
您不会收到任何错误消息
因此,如果你只想override
一些方法,使用KeyAdapter
很方便。