Results 1 to 8 of 8
  1. #1
    siyi90 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default KeyListener problem

    hi guys , I am trying to make a game for my project....but I am encountering some problem......Any help would be appreciated.




    so , the game class is working when I run it individually ,the keylistener is working as well....
    However when I run it using a Frame which would instatiate a welcome panel of my game , which have a button leading to the game page, the keylistener(up,down,left,right) does not work anymore ,anyone know what is the problem?
    Last edited by siyi90; 02-07-2010 at 10:45 AM.

  2. #2
    siyi90 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    JFrame called MainFrame :
    Java Code:
    //import
    public class MainFrame extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    
    	public static void main(String[]args){
    		new MainFrame();
    	}
    	public MainFrame() {
    		super();
    		initialize();
    		JPanel panel = new WelcomePanel(this);
    		this.getContentPane().add(panel);
    		this.setVisible(true);
    	}
    
    	private void initialize() {
    		this.setSize(820, 578);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setContentPane(getJContentPane());
    		this.setTitle("Jewel Hunt");
            this.setLocationRelativeTo(null);
    	}
    	JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    
    		}
    		return jContentPane;
    	}
    	
    }
    The Welcome Panel
    Java Code:
    //import
    
    public class WelcomePanel extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    	private JLabel jLabelWelcome = null;
    	private MainFrame myFrame = null;
    	private JButton jButtonStraight = null;
    
    	public WelcomePanel(MainFrame f) {
    		super();
    		myFrame = f;
    		initialize();
    	}
    
    	private void initialize() {
    		
    		//some setting
    
    	       this.setFocusable(true);
    		this.add(getJButtonStraight(), null);
    		this.setFocusable(true);
    		
    	
    	private JButton getJButtonStraight() {
    		if (jButtonStraight == null) {
    			jButtonStraight = new JButton();
    			jButtonStraight.setBounds(new Rectangle(79, 160, 91,45));
    			jButtonStraight.setText("Bonus");
    			jButtonStraight.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					System.out.println("actionPerformed()");
    myFrame.setFocusable(true);
    Bonus bonus = new Bonus(myFrame);
    bonus.setFocusable(true);
    					myFrame.getJContentPane().removeAll();
    					myFrame.getJContentPane().add(bonus);
    					
    					myFrame.getContentPane().validate();
    					myFrame.getContentPane().repaint();
    				}
    			});
    		}
    		return jButtonStraight;
    	}
    }
    The Game called bonus.java (the job of the keylistener is to move image of a character you control)
    something like a snake game/pacman game(except that there no ghost that move)
    Java Code:
    //import
    public class Bonus extends JPanel implements ActionListener {
    
    	private static final long serialVersionUID = 1L;
        private MainFrame myFrame= null;
    
    //some declaration of variables
    //some timer methods for the game
    
    	public Bonus(MainFrame f) {
            this.addKeyListener(new TAdapter());
            this.myFrame=f;
            initialize();
    
            this.setBackground(Color.black);
    
            //images setting for the game
    
            this.setFocusable(true);
            countdown.start();
            initGame();
            
        }
    			
    
    	 
    	private void initialize() {
    
    		//more images setting and component setting
    	}
    
    
    	public void initGame() {
    //initializing of game methods
        }
    
    
        public void paint(Graphics g) {
           //drawing of images
        }
    
    
        public void gameOver(Graphics g) {
           //one of the game method
            
        }
    
    
        public void checktreasure() {
    //another game method
        }
    
    
        public void move() {
            //moving of images after keylistener
        }
    
    
        public void checkCollision() {
    //game method
        }
    
        public void locatetreasure() {
          //game method
        }
    
        public void actionPerformed(ActionEvent e) {
            if (inGame) {
                checktreasure();
                checkCollision();
                move();
            }
            repaint();
        }
    
    
        private class TAdapter extends KeyAdapter {
    
            public void keyPressed(KeyEvent e) {
            	
                int key = e.getKeyCode();
    
                if ((key == KeyEvent.VK_LEFT) && (!right)) {
                    left = true;
                    up = false;
                    down = false;
                    System.out.println("left");
                }
    
                if ((key == KeyEvent.VK_RIGHT) && (!left)) {
                    right = true;
                    up = false;
                    down = false;
                }
    
                if ((key == KeyEvent.VK_UP) && (!down)) {
                    up = true;
                    right = false;
                    left = false;
                }
    
                if ((key == KeyEvent.VK_DOWN) && (!up)) {
                    down = true;
                    right = false;
                    left = false;
                }
                }
        }
    	//buttons getter
    	}
    }
    the bonus.java keylistener was working , I was able to control the character with the arrow keys when I run it as a individual class...

    However , when run the mainframe class first...The welcome panel shows up, When I clicked on the button leading to the game(bonus.java) , the panel made in bonus.java comes up , the game was running fine(timer and such) , but I was unable to move my character with the arrows key
    Please help me with this.....only a few hours left before I am to present on this game

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It could be a focus problem which is an issue with key listeners. I'd suggest that you use key binding instead (see the Sun Swing tutorial on this). It would likely solve your issues.

  4. #4
    siyi90 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    I also think that it was a focusable problem....but I was not sure as I am quite new to java...
    About the key binding , I tried reading the tutorial but I was unclear about how to do it...

    Thank anyway.
    Any other suggestions or solution?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by siyi90 View Post
    Any other suggestions or solution?
    ???
    How do you think this is going to help?

    I hate to say it, but key binding is the correct solution. Your best bet is to re-look at the tutorial and come back to discuss any sections you don't understand.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, your class as posted above won't compile for me, so I can't make any more specific recs.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also: I have an example of using key bindings to move an image here: key binding example

  8. #8
    siyi90 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    I found the problem.
    It was because the mainFrame was needed to be set focused when initializing the program through mainframe...

    thx anyway furbarable

Similar Threads

  1. Problem with KeyListener in Jtable
    By sandeepsai17 in forum New To Java
    Replies: 0
    Last Post: 06-30-2009, 10:14 AM
  2. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  3. Unfocused keylistener
    By rolfrolf in forum New To Java
    Replies: 3
    Last Post: 11-06-2008, 09:21 AM
  4. KeyListener Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 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
  •