problem with KeyListener

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankajs
    New Member
    • Mar 2008
    • 36

    problem with KeyListener

    Hello freinds! I am developing a game & a problem occur with pressing the keys
    the class name is game it extends JPanel, when I register keylistener by writing addKeyListener( this); in the constructer game(),it's not working but when I extends JFrame it starts working & my problem is I have to extends JPanel ,so how can I solve it.....

    Here is the code

    import javax.swing.*;
    public class Game extends JPanel
    {
    public BufferedImage image,buffer;

    public Game(BufferedIm age image)
    {
    this.image=imag e;
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Why do you think you need to use a KeyListener? Give your requirements. I ask because my rule of thumb is that KeyListener is never the answer. Keyboard binding is:

    Comment

    • pankajs
      New Member
      • Mar 2008
      • 36

      #3
      ThankYou! for reply but take a look at my source code in my new post titled
      keyListener problem with code

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by pankajs
        ThankYou! for reply but take a look at my source code in my new post titled
        keyListener problem with code
        I think keyboard bindings are much better than KeyListener. You may also have to set:

        [CODE=Java]setFocusable(tr ue);[/CODE]

        Comment

        • pankajs
          New Member
          • Mar 2008
          • 36

          #5
          Oh ThankYou Very Much!

          Now it works by adding setFocusable(tr ue);

          Comment

          Working...