我有一个简单的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());
}
列表到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.
}
}
正如你所看到的那样按下向上箭头键应该说像Hi,但没有任何反应。 如果我尝试在Gui类中添加(new Key()),则会出错。 我做错了什么?
谢谢
答案 0 :(得分:2)
您需要先阅读教程。您不会使用add(...)
添加KeyListeners,而是使用addKeyListener(...)
添加add(...)
,因为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");
}
});
}
}
仅用于添加组件,但话虽如此,我甚至不会使用一个KeyListener,而不是Key Bindings,这将有助于解决焦点阻止KeyListener工作时将来会遇到的问题。
例如,使用我的小键绑定添加编译并运行Kevin Bowersox的程序:
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);
}
}
如果你运行它并按向上和向下箭头,你会看到来自正常运行的KeyListener和Key Bindings的响应。
但是,如果你像这样添加一个JButton:
{{1}}
然后按下按钮后会发生什么?告诉我哪个关键处理程序有效?
答案 1 :(得分:1)
您需要使用addKeyListener()
方法:
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);
}
}