开发者

Do i need to extend Adapter Class 'here'?

开发者 https://www.devze.com 2023-02-27 02:56 出处:网络
This is my snippet : tf.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent ke) { getEnter(ke);

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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号