我希望在按下转义时清除文本字段。
我将KeyListener添加到JTextField
bondIDTextField.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if( e.getKeyCode() == KeyEvent.VK_ESCAPE)
((JTextField)e.getSource()).setText("");
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个代码。正在运作
bondIDTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
((JTextField)e.getSource()).setText("");
}
}
}
);