Hello there Daniweb community, i am creating a 2d game in Java, however i have a problem. I googled up KeyListeners for JFrame, as they didnt work as usual, and i found out that when using KeyListeners with JFrame you have to set focus, right?
Well. I have Googled abit now, but cant seem to find a solution to my problem. If you guys could take a look at my codepiece, that would be a great help for me.
package com.amellingsen.erlend;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class BoxWorld extends JFrame implements KeyListener{
public BoxWorld()
{
setFocusable(true);
add(new Board());
setTitle("BoxWorld");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(700, 700);
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}
public static void main(String[] args)
{
new BoxWorld();
}
public void keyPressed(KeyEvent e) {
System.out.println("KeyPressed");
}
public void keyReleased(KeyEvent e) {
System.out.println("KeyReleased");
}
public void keyTyped(KeyEvent e) {
System.out.println("KeyTyped");
}
}
Thanks in advance :)