• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

keyevent not working in applet?

 
ajay smith
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just started creating this portal game and I've encountered no problems except that KeyEvent isn't doing anything. Here's my code so far (in two classes):



and the second class:



Help please?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add "keyaction" as KeyListener to the component that should listen to key events.
 
ajay smith
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do I put that in/how do I do that? Kinda confused
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like "addKeyListener(new keyaction())". You would do that in the init method. The thinkerbox method you should delete entirely. And the keyaction class needs to override one (or more) of the 3 methods it defines, not create a new method. And it must not create a new instance of the portal class.

Start by reading http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html to get some understanding how key listeners work.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AWT was superseded by Swing more than 10 years ago. I recommend you change to Swing, which means extending JApplet instead of Applet and gaining a basic understanding of the component hierarchy imposed by the RootPaneContainer interface. Then you can leverage Swing's Key Bindings which afford much more flexibility than a simple KeyListener.

db
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic