Reputation: 51
I've taken up learning Java. At the moment, I'm trying to get a key listener to work but I am struggling. My code is below. There is an error at the first curly bracket at the beginning of the anonymous inner class. I'm probably missing something simple, so any help would be useful.
public class Klistener extends JFrame {
void Klistener()
{
JPanel c = new JPanel();
c.setFocusTraversalKeysEnabled(true);
this.add(c);
c.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
System.out.print("hi");
}
}
});
}
public static void main(String[] args)
{
Klistener a = new Klistener();
JFrame b = new JFrame("hi");
b.setVisible(true);
b.setSize(500, 500);
b.setLocation(500, 200);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Upvotes: 1
Views: 515
Reputation: 331
You got some mistakes there. Here is a working code (Minimum changes):
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Klistener extends JFrame {
public Klistener()
{
JPanel c = new JPanel();
c.setFocusTraversalKeysEnabled(true);
addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e){}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_DOWN)
System.out.println("hi");
}
@Override
public void keyReleased(KeyEvent e){}
});
}
public static void main(String[] args)
{
Klistener a = new Klistener();
a.setSize(500, 500);
a.setLocation(500, 200);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setVisible(true);
}
}
Upvotes: 1
Reputation: 14806
You have so many mistakes so I don't even know were to start:
1) KeyListener
is an interface so you have to implement all method's.
2) Don't add KeyListener
to panel, add it to your JFrame
. You can do that by creating anonymus class or this way
public class Klistener extends JFrame implements KeyListener{}
3) Don't call setSize()
method, call pack()
and setPreferredSize()
method's and after that setVisible(true)
, and call them in the end of the code.
This is how your main method should look:
public static void main(String[] args){
Klistener k = new Klistener();
k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
k.setLocationByPlatform(true);
k.setPreferredSize(new Dimension(400,300));
k.pack();
k.setVisible(true);
}
Upvotes: 2
Reputation: 6783
Since KeyListener
is an interface
, your inner class needs to override all the methods that have been declared in the interface. The interface specifies the following methods:
public interface KeyListener extends EventListener {
/**
* Invoked when a key has been typed.
* See the class description for {@link KeyEvent} for a definition of
* a key typed event.
*/
public void keyTyped(KeyEvent e);
/**
* Invoked when a key has been pressed.
* See the class description for {@link KeyEvent} for a definition of
* a key pressed event.
*/
public void keyPressed(KeyEvent e);
/**
* Invoked when a key has been released.
* See the class description for {@link KeyEvent} for a definition of
* a key released event.
*/
public void keyReleased(KeyEvent e);
}
A better way is to use the KeyAdapter
abstract
adapter class for receiving keyboard events. Over here, you can over-ride any specific method
that you want and not all.
Also, since you're new to Java
Swing
, the following links might come in handy:
And oh, I noticed that you've a method void Klistener
in the class named Klistener
. I'm assuming that you're trying to make a constructor
and not a method per se. If this is the case, then realize that constructors
do not have any return type! Here's yet another link that might be of help.
Upvotes: 3
Reputation: 44808
You forgot to provide implementations for the other two methods of KeyListener
. If you don't want to do that, extend KeyAdapter
instead of KeyListener
. KeyAdapter
provides empty implementations of all of KeyListener
's methods so you can pick and choose which ones to implement.
Upvotes: 3