| Author |
Adding a KeyListener to a JLabel?
|
Josh Rodrigues
Greenhorn
Joined: Apr 14, 2010
Posts: 17
|
|
Well, I'm making a game and I'm trying to add a KeyListener to a JLabel which contains an ImageIcon. Every time I press the up, down, right or left arrow key, they print out what I want it to print out, but do not do the action I wish for them to perform. Here's my code:
I tried adding the requestFocus() method and a few different ways of creating the keyPressed(KeyEvent e) method. Help is appreciated. Thanks.
|
 |
Olivier Legat
Ranch Hand
Joined: Nov 17, 2007
Posts: 176
|
|
Hello Josh,
I don't see why anything should happen other than printing the text. In the keyPressed methods you are incrementing/decrementing the variables xPos and yPos, which are not ever used locally (other than in keyPressed). Unless these variables are used elsewhere, these variations are useless.
|
Olly
|
 |
 |
|
|
subject: Adding a KeyListener to a JLabel?
|
|
|