所以我试图诚实地使用 KeyBoardEvent 只是为了测试目的.. 我所做的似乎并不奏效:/希望你们能告诉我我在哪里 derping。谢谢!
package com.dillyg10.test.Keys;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Keys extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
public Keys(){
}
public static void registerEvent(Window w){
w.setFocusTraversalKeysEnabled(false);
w.setFocusable(true);
w.addKeyListener(new Keys());
}
public void keyPressed(KeyEvent e) {
System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void gui() {
JFrame frame = new JFrame("Keys");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
registerEvent(new Window(frame));
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
gui();
}
});
}
}
这段代码基本上是一个测试,以确定键盘事件是否有效,我只是在按下时打印出键..但没有打印任何内容。