Results 1 to 4 of 4
  1. #1
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default adding keylistener to JFrame

    Ok im making a basic game here is my code:
    Java Code:
    package City;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class City implements KeyListener{
        
            Player p = new Player();
            Map map = new Map();
            
        public static void main(String[] args){
    
            JFrame GameFrame = new JFrame("City");
            GameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GameFrame.setSize(800,600);
            GameFrame.setVisible(true);
            GameFrame.setResizable(false);
            GameFrame.addKeyListener(this);
        }
        
        public void MenuOn(){
            
            
            
        }
    
        public void MenuOff(){
            
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
            switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                    p.moveLeft();
                        break;
                    case KeyEvent.VK_RIGHT:
                    p.moveRight();
                        break;
                    case KeyEvent.VK_UP:
                    p.moveUp();
                        break;
                    case KeyEvent.VK_DOWN:
                    p.moveDown();
                        break;       
                    }
            }
    
        @Override
        public void keyPressed(KeyEvent e) {
            
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            
        }
        
    }
    I'm a error by GameFrame.addKeyListener(this); non-static variable, well im not using a variable...any ideas?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: adding keylistener to JFrame

    1. Learn to follow coding conventions so your code isn't confusing to other programmers: Code Conventions for the Java Programming Language: Contents

    2. You're referring to "this" which is a reference to the current instance of the class, from a static method that isn't tuied to any particular instance. If you don't understand that, take a step back and learn the fundamentals of Java before progressing to GUIs.

    3. Adding a KeyListener to a JFrame isn't going to get you anywhere, since a JFrame is completely covered by its rootPane, which in turn has children. KeyEvents are only delivered to the component that has the focus.

    4. Swing components are designed to use key bindings. Once you understand the basics, search for the Oracle tutorial on How to Use Key Bindings and follow that route.

    Since the primary issue here is that a non-static member cannot be accessed from a static method, I'm retaining this in New to Java. If and when you reach a stage that you have a genuine AWT/Swing problem, please post in the appropriate section of the forum.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: adding keylistener to JFrame

    Sorry i wasnt thinking much i moved jframe to a new method and called that method and keylistener now works and i also added a pane.
    Thanks for your help

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: adding keylistener to JFrame

    Quote Originally Posted by DarrylBurke View Post
    4. Swing components are designed to use key bindings. Once you understand the basics, search for the Oracle tutorial on How to Use Key Bindings and follow that route.
    Quote Originally Posted by Alkor View Post
    Sorry i wasnt thinking much i moved jframe to a new method and called that method and keylistener now works and i also added a pane.
    So, you prefer to do things the wrong way. I'll try to remember that the next time you need help.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help: adding keylistener to a thread
    By Hooiser in forum Threads and Synchronization
    Replies: 16
    Last Post: 07-26-2011, 11:17 PM
  2. Adding keyListener to a 2D object.
    By Guy in forum New To Java
    Replies: 2
    Last Post: 07-24-2011, 09:20 PM
  3. adding keylistener
    By natdizzle in forum AWT / Swing
    Replies: 2
    Last Post: 02-02-2011, 02:22 AM
  4. Adding a KeyListener to a JFrame with buttons.
    By jamhead in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 08:29 PM
  5. Help me in adding keylistener
    By kumarv75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-22-2010, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •