KeyListener in Java is abstract; unable to instantiate?

I try to create a Key Listener in java, but when I try

KeyListener listener = new KeyListener();

Netbeans tells me that KeyListener is abstract, cannot be created. I know that I don’t have any other part of this listener, but since this is my first time using a key listener, I’m not sure what else I need. Why does this tell me that?

Thank,

Tomek

+3
source share
3 answers

KeyListener- this is an interface - it must be implemented by something. So you can do:

KeyListener listener = new SomeKeyListenerImplementation();

but you cannot create it directly. You can use an anonymous inner class:

KeyListener listener = new KeyListener()
{
    public void keyPressed(KeyEvent e) { /* ... */ }

    public void keyReleased(KeyEvent e) { /* ... */ }

    public void keyTyped(KeyEvent e) { /* ... */ }
};

It depends on what you want to do, basically.

+7
source

KeyListener - , , , . , , inline, KeyAdapter, , KeyListener, . KeyAdapter, , , . , keyPressed,

KeyListener listener = new KeyAdapter()
{
    public void keyPressed(KeyEvent e) { /* ... */ }
};

.

+5

KeyListener - , , . , tutorial.

+1

Source: https://habr.com/ru/post/1699502/


All Articles