Results 1 to 4 of 4
Thread: adding keylistener to JFrame
- 03-08-2012, 05:28 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 39
- Rep Power
- 0
adding keylistener to JFrame
Ok im making a basic game here is my code:
I'm a error by GameFrame.addKeyListener(this); non-static variable, well im not using a variable...any ideas?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) { } }
- 03-08-2012, 05:46 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-08-2012, 06:22 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 39
- Rep Power
- 0
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
- 03-08-2012, 12:02 PM #4
Similar Threads
-
Help: adding keylistener to a thread
By Hooiser in forum Threads and SynchronizationReplies: 16Last Post: 07-26-2011, 10:17 PM -
Adding keyListener to a 2D object.
By Guy in forum New To JavaReplies: 2Last Post: 07-24-2011, 08:20 PM -
adding keylistener
By natdizzle in forum AWT / SwingReplies: 2Last Post: 02-02-2011, 01:22 AM -
Adding a KeyListener to a JFrame with buttons.
By jamhead in forum AWT / SwingReplies: 1Last Post: 12-11-2010, 07:29 PM -
Help me in adding keylistener
By kumarv75 in forum CLDC and MIDPReplies: 0Last Post: 06-22-2010, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks