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
|
|
|
|
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!  ) Also I tried adding Mouse listener and it didnt work?
|
|
|
|
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!
|
|
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
|
|
|
|
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.
|
|
|
|
maxking1234
Senior Newbie 
|
 |
«
Reply #5 - Posted
2012-04-21 13:22:33 » |
|
My JFrame Doesnt extends canvas? Is that what you mean?
|
|
|
|
ReBirth
|
 |
«
Reply #6 - Posted
2012-04-21 13:39:04 » |
|
Post full code that extend JFrame.
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
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){ frame.addKeyListener(new InputHandler()); }
public static void main(String[] args){ new Display(this); } |
|
|
|
|
Games published by our own members! Check 'em out!
|
|
65K
|
 |
«
Reply #10 - Posted
2012-04-22 06:20:35 » |
|
Try requestFocus().
|
Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
|
|
|
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); createBufferStrategy(2); Game game = new Game(this.getBufferStrategy()); setFocusable(true); addKeyListener(new KL()); requestFocus(); add(game); pack(); setVisible(true); setLocationRelativeTo(null); }
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.
|
|
|
|
ra4king
|
 |
«
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
|
|
|
|
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.
|
|
|
|
|