【问题标题】:KeyTyped event doesn't recognize colon keyKeyTyped 事件无法识别冒号键
【发布时间】:2013-12-19 05:02:31
【问题描述】:

我试图在 jTextField 中只接受数字 (0...9) 和 ':'(冒号)键,但它不接受冒号键。这是为什么呢?

我的代码是:

private void horaInicioKeyTyped(java.awt.event.KeyEvent evt) {
    char c=evt.getKeyChar();
    if(!(Character.isDigit(c) || c== KeyEvent.VK_BACK_SPACE || 
         c==KeyEvent.VK_DELETE || (c==KeyEvent.VK_COLON ))){
            evt.consume();
            getToolkit().beep();

    }
}

【问题讨论】:

  • 1) 永远不要在 JTextField 中使用 KeyListener。 2) 考虑使用 JFormattedTextField。 3) 或 DocumentFilter。
  • 不是冒号和shift+的组合; ?但我同意气垫船
  • 好的,我会采纳建议的,谢谢

标签: java keyevent


【解决方案1】:

不要使用 VK_COLON,像这样使用 ':' -

if (Character.isDigit(c) || c==':' || 
      c==KeyEvent.VK_BACK_SPACE || c==KeyEvent.VK_DELETE) {
  evt.consume();
  getToolkit().beep();
}

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2012-12-08
    • 2020-02-28
    相关资源
    最近更新 更多