如何更改或隐藏该 JTextField 以不向用户显示并仍然获得用户键入的键?我尝试使用“KeyCodeT.setVisible(false);”,但它没有用。
这就是我所说的:http: //oi46.tinypic.com/35hobbn.jpg
完整代码在这里: http: //pastebin.com/8t3cTLKX
JTextField KeyCodeT = new JTextField("Key Code:");
public KeyProgram() {
KeyCodeT.addKeyListener(this);
KeyCodeT.setEditable(false);
add(KeyCodeT);
setSize(300, 300);
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed!!!");
if (e.getKeyCode() == 27) {
JOptionPane.showMessageDialog(null, "Good Bye");
System.exit(0);
}
}
public void keyReleased(KeyEvent e) {
System.out.println("Key Released!!!");
KeyCodeT.setText("Key Code:" + e.getKeyCode());
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
KeyProgram key = new KeyProgram();
key.setVisible(true);
}