I'm making a game and I am wondering how you add a keylistener to a jpanel (How to get key input from the user without using a textfield or textarea or anything like that)?
I'm using the timer class (javax.swing.timer), and in the constructor of my own class that I made, I typed:
addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e){
int key=e.getKeyCode();
if(key==KeyEvent.VK_DOWN){
y+=movey;
}
}
@Override
public void keyReleased(KeyEvent e){
}
@Override
public void keyTyped(KeyEvent e){
}
});
It doesn't work with this code for some reason. Any suggestions? It doesn't matter what key is being hit, I just want to know how to add a keylistener to the JPanel.
Thanks