Reputation: 23
I have a simple Gui
public Gui(){
ablak = new JFrame("Snake game");
ablak.setVisible(true);
ablak.setSize(new Dimension(600,600));
ablak.setFocusable(true);
ablak.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ablak.add(new Grid());
ablak.add(new Key());
}
And a key class to listner to keyevents
package snake;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Key implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
System.out.println("Hi");
}
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
As you can see pressing the Up arrow key should say something like Hi, but nothing happens. If i try to add(new Key()) in Gui class it gives an error. What am i doing wrong?
Thank you
Upvotes: 1
Views: 20842
Reputation: 285440
You need to read the tutorials first. You don't add KeyListeners with add(...)
but rather with addKeyListener(...)
since add(...)
is for adding component only, but having said that, I wouldn't even use a KeyListener for this but rather Key Bindings which would help to fix the problems you will have in the future when focus prevents your KeyListener from working.
For example, compile and run Kevin Bowersox's program with my little Key Binding addtion:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test2 {
public static void main(String[] args) {
JFrame ablak = new JFrame("Snake game");
ablak.setVisible(true);
ablak.setSize(new Dimension(600, 600));
ablak.setFocusable(true);
ablak.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ablak.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("Hi from KeyListener");
}
}
});
ablak.setVisible(true);
JPanel contentPane = (JPanel) ablak.getContentPane();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = contentPane.getInputMap(condition);
ActionMap actionMap = contentPane.getActionMap();
String down = "down";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), down);
actionMap.put(down, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Hi from Key Binding");
}
});
}
}
If you run it and press the up and down arrows, you'll see the responses from both a functioning KeyListener and Key Bindings.
But what if you add a JButton to the mix like so:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test2 {
public static void main(String[] args) {
JFrame ablak = new JFrame("Snake game");
ablak.setVisible(true);
ablak.setSize(new Dimension(600, 600));
ablak.setFocusable(true);
ablak.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ablak.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("Hi from KeyListener");
}
}
});
JPanel contentPane = (JPanel) ablak.getContentPane();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = contentPane.getInputMap(condition);
ActionMap actionMap = contentPane.getActionMap();
String down = "down";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), down);
actionMap.put(down, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Hi from Key Binding");
}
});
JButton fooButton = new JButton("Foo");
contentPane.setLayout(new FlowLayout());
contentPane.add(fooButton);
ablak.setVisible(true);
}
}
And then what happens after you press the button? Tell me which key handling routine works?
Upvotes: 2
Reputation: 94499
You need to use the addKeyListener()
method:
public class Test2 {
public static void main(String[] args) {
JFrame ablak = new JFrame("Snake game");
ablak.setVisible(true);
ablak.setSize(new Dimension(600,600));
ablak.setFocusable(true);
ablak.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ablak.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
System.out.println("Hi");
}
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
ablak.setVisible(true);
}
}
Upvotes: 3