我如何在 Java 中向我的 Gui 添加一个密钥侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20413910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i add a keylistener to my Gui in java
提问by user3066227
I have a simple Gui
我有一个简单的 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
和一个关键的类来监听 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?
正如您所看到的,按向上箭头键应该会显示“嗨”之类的内容,但没有任何反应。如果我尝试在 Gui 类中添加 (new Key()) 它会出错。我究竟做错了什么?
Thank you
谢谢
采纳答案by Kevin Bowersox
You need to use the addKeyListener()
method:
您需要使用以下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);
}
}
回答by Hovercraft Full Of Eels
You need to read the tutorials first. You don't add KeyListeners with add(...)
but rather with addKeyListener(...)
since add(...)
is for adding componentonly, 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.
您需要先阅读教程。您不添加 KeyListeners withadd(...)
而是 withaddKeyListener(...)
因为add(...)
仅用于添加组件,但话虽如此,我什至不会为此使用 KeyListeners 而是使用 Key Bindings 这将有助于解决您将来遇到的问题焦点阻止您的 KeyListener 工作。
For example, compile and run Kevin Bowersox's program with my little Key Binding addtion:
例如,编译并运行 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");
}
}
});
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.
如果您运行它并按下向上和向下箭头,您将看到来自正常运行的 KeyListener 和 Key Bindings 的响应。
But what if you add a JButton to the mix like so:
但是,如果您像这样将 JButton 添加到组合中会怎样:
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?
然后按下按钮后会发生什么?告诉我哪个键处理例程有效?