This is my snippet :
tf.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent ke) {
getEnter(ke);
}
});
There are 3 methods in KeyListener interface
.But开发者_如何学JAVA i want to use only one method i.e keyTyped()
How can i do this?
(Do i need to extend AdapterClass? if yes, how?)
Yes, you need to extend the KeyAdapter
class. It provides (empty) implementations of all the KeyListener
methods. You have to override the one(s) you need and the empty implementations will satisfy the interface's contract. You'd extend it the same way as in your code:
tf.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
getEnter(ke);
}
});
As the Javadoc itself for KeyAdapter says:
Extend this class to create a KeyEvent listener and override the methods for the events of interest. (If you implement the KeyListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)
精彩评论