GeeCON Prague 2014*
The moose likes Game Development and the fly likes Adding a KeyListener to a JLabel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Game Development
Bookmark "Adding a KeyListener to a JLabel?" Watch "Adding a KeyListener to a JLabel?" New topic
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
 
GeeCON Prague 2014
 
subject: Adding a KeyListener to a JLabel?