Results 1 to 3 of 3
  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Post adding keylistener

    I got this program to make a JFrame, and let me draw on it. Now im trying to create some sort of keylistener that detects up left right down keys. It's acting like its not calling the user input class

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class square{
    
    	public static void main(String[] args) {
    		new square();
    	}
    	public square(){
    		JFrame panel = new JFrame();
    		panel.setExtendedState(panel.MAXIMIZED_BOTH);
    		panel.setSize(600, 400);
    		panel.setVisible(true);
    		panel.getContentPane().add(new painting());
    	}
    	public class painting extends JComponent{
    	    public void paint(Graphics g){
    	      g.setColor(Color.red);
    	      g.fillRect(10,10,220,225);
    	    }
    	  }
    	
    	public class input implements KeyListener{
    		boolean down;
    		@Override
    		public void keyPressed(KeyEvent e) {
    			int keyCode = e.getKeyCode();
    			if (keyCode == KeyEvent.VK_DOWN){
    	        down = true;
    			}
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    		
    	}
    }
    Last edited by natdizzle; 02-02-2011 at 02:21 AM.

  2. #2
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Post

    its like im not instanciating the user input class

  3. #3
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

Similar Threads

  1. Help adding a keylistener to a paint class
    By Phill Phan in forum New To Java
    Replies: 3
    Last Post: 01-20-2011, 10:28 PM
  2. Adding a KeyListener to a JFrame with buttons.
    By jamhead in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 08:29 PM
  3. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 01:55 AM
  4. Help me in adding keylistener
    By kumarv75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-22-2010, 08:10 AM
  5. KeyListener Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:46 PM

Posting Permissions

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