Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: keyPressed method not working?

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default keyPressed method not working?

    Hi, I have this code:

    public void keyPressed(KeyEvent e){
     
    			int keyCode = e.getKeyCode();
    			if (keyCode == KeyEvent.VK_ESCAPE){
     
    				System.exit(0);
     
    			}
     
    		}

    But I am getting "illegal start of expression" and "';' expected" errors, I guess I am doing something wrong?

    I have implemented the KeyListener class.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: keyPressed method not working?

    You're going to have to supply an SSCCE that demonstrates the error- it could be that the source of your problem is in another piece of code (probably a missing bracket or parenthesis). Boil it down to as small a piece of code that still exhibits that behavior, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keyPressed method not working?

    This is my code, it is only small:

    import java.awt.*;
    import javax.swing.*; 
    import java.awt.event.*;
     
    class MoveIcon implements KeyListener{
     
    	public static void main(String[]args){
    		//create window
    		JFrame frame = new JFrame("Moving Icon App");
    		frame.setSize(600,400);
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
     
    		JPanel background = new JPanel();
    		background.setOpaque(true);
     
    		frame.setContentPane(background);
    		frame.setLayout(null);
     
    		ImageIcon icon = new ImageIcon("dot.jpg");
     
     
    		JLabel label = new JLabel(icon);
    		label.setSize(label.getPreferredSize());
    		label.setLocation(100,50);
     
     
    		Container windowsContentPane = frame.getContentPane();
     
    		windowsContentPane.add(label);
     
     
    		frame.setVisible(true);
     
    		public void keyPressed(KeyEvent e){
     
    			int keyCode = e.getKeyCode();
    			if (keyCode == KeyEvent.VK_ESCAPE){
     
    				System.exit(0);
     
    			}
     
    		}
     
     
     
     
    	}
     
     
     
     
     
    }

    Thanks

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: keyPressed method not working?

    Ah, I see. Your keyPressed() method seems to be inside your main() method, which is not allowed. You could have an anonymous inner class, but I don't think that's what you're going for, since you implemented KeyListener.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keyPressed method not working?

    Ah thank you, ive now got it to compile but it doesnt work.

    I have to defined all of the methods of the KeyListener interface. I have removed the "if (keyCode == KeyEvent.VK_ESCAPE){" and just put in "System.out.print("hi");", I also tried "System.exit(0);", but nothing happens. Ideas?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: keyPressed method not working?

    When do you add the KeyListener? Recommended reading: How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. random not working in .class method
    By Spidey1980 in forum Java SE APIs
    Replies: 13
    Last Post: August 19th, 2011, 07:35 AM
  2. method not working outside of class
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2011, 10:00 AM
  3. contains method not working?
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2011, 01:57 PM
  4. Help with Graphics class and keyPressed method
    By smashX in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 10th, 2011, 12:46 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM