aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding a KeyListener to a JLabel?
 
Similar Threads
applet to change an image when keypressed
Problems loading image into a JLabel located in a JScrollPane
Click On Image.
JPanel painComponent not working
spalsh screen