Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JFrame KeyListener Problem  (Read 3352 times)
0 Members and 1 Guest are viewing this topic.
Offline maxking1234

Senior Newbie





« Posted 2012-04-21 08:38:06 »

Hi, My KeyListner is not working for my JFrame and JPanel and I can't work out why.
I am using the exact code that has worked for me before
Please help!
Here is the JFrame
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public Display() {
      setSize(640, 480);
      setTitle("Ludum Dare 23: ");
      setVisible(true);
      setResizable(false);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      createBufferStrategy(2);
      Game game = new Game(this.getBufferStrategy());
      setFocusable(true);
      addKeyListener(new InputManager());
      add(game);
   }

Max
Offline maxking1234

Senior Newbie





« Reply #1 - Posted 2012-04-21 11:28:29 »

Anyone? (Please note this is for the Ludum Dare! so I kind of need it fast! Smiley )
Also I tried adding Mouse listener and it didnt work?
Offline ReBirth
« Reply #2 - Posted 2012-04-21 13:02:42 »

Usually we add it to container (JPanel or Canvas). More since its KeyListener problem you should post your InputManager class. I'm in LD23 too.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline maxking1234

Senior Newbie





« Reply #3 - Posted 2012-04-21 13:09:00 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class InputManager extends KeyAdapter{

   public void keyPressed(KeyEvent key) {
      System.out.println(key.getKeyChar());
   }

   public void keyReleased(KeyEvent key) {
      System.out.println(key.getKeyChar());
     
   }

   public void keyTyped(KeyEvent key) {
      System.out.println(key.getKeyChar());
     
   }
}

This is just to test it
Offline ReBirth
« Reply #4 - Posted 2012-04-21 13:18:23 »

Hmm nothing wrong, except InputManager class isn't common to be public since we place it to the class that need it so it can access game resource from that class.

Wait, you said JFrame but your method show that like you extend Canvas.

Offline maxking1234

Senior Newbie





« Reply #5 - Posted 2012-04-21 13:22:33 »

My JFrame Doesnt extends canvas?
Is that what you mean?
Offline ReBirth
« Reply #6 - Posted 2012-04-21 13:39:04 »

Post full code that extend JFrame.

Offline maxking1234

Senior Newbie





« Reply #7 - Posted 2012-04-21 13:45:24 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
package uk.co.maxkingstudios.ld23;

import javax.swing.JFrame;

public class Display extends JFrame {

   private static final long serialVersionUID = 1L;

   public Display() {
      setSize(640, 480);
      setTitle("Ludum Dare 23: ");
      setVisible(true);
      setResizable(false);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      createBufferStrategy(2);
      Game game = new Game(this.getBufferStrategy());
      add(game);
   }

   public static void main(String[] args) {
      new Display();
   }
}

This is the JFrame class
Offline ReBirth
« Reply #8 - Posted 2012-04-21 13:56:58 »

Hmm weird since nothing wrong. I'll share you mine (extending applet)
1  
2  
3  
4  
5  
6  
7  
canvas.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);
      this.add(canvas);
      addKeyListener(new KeyInput());
      canvas.createBufferStrategy(2);
      strategy = canvas.getBufferStrategy();

This's used on my previous jam.

Offline Longarmx
« Reply #9 - Posted 2012-04-22 05:17:41 »

You have to do this even though you extend JFrame:

1  
2  
3  
4  
5  
6  
7  
8  
public Display(JFrame frame){
     // Other stuff
     frame.addKeyListener(new InputHandler());
}

public static void main(String[] args){
     new Display(this);
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #10 - Posted 2012-04-22 06:20:35 »

Try requestFocus().

Offline GabrielBailey74
« Reply #11 - Posted 2012-04-22 07:43:06 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
public Display() {
      setSize(640, 480);
      setTitle("Ludum Dare 23: ");
      setResizable(false);
      setDefaultCloseOperation(3); // 3 == EXIT_ON_CLOSE
      createBufferStrategy(2);
      Game game = new Game(this.getBufferStrategy());
      setFocusable(true); // setfocusable before adding any listeners
      addKeyListener(new KL()); // Adding the KeyListener/Adapter created below
      requestFocus(); // request initial focus
      add(game);
      pack(); // final frame init
      setVisible(true); // frames visible after all is added
      setLocationRelativeTo(null); // Move frame to center after visible
}

// New inner class that handles KeyEvents
public class KL extends KeyAdapter {
      public void keyPressed(KeyEvent e) {
      }
      public void keyReleased(KeyEvent e) {
      }
      public void keyTyped(KeyEvent e) {
      }
}


Just realized you already had a class of a KeyAdapter, oops.

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2012-04-22 15:14:29 »

You have to add the listeners on the component that you draw on, not the JFrame

Offline ReBirth
« Reply #13 - Posted 2012-04-23 00:13:20 »

You have to add the listeners on the component that you draw on, not the JFrame

That's what I said first.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (94 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (151 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!