Is there any way to accept only numeric values in a JTextField?

前端 未结 19 1882
陌清茗
陌清茗 2020-11-22 03:10

Is there any way to accept only numeric values in a JTextField? Is there any special method for this?

19条回答
  •  既然无缘
    2020-11-22 04:16

    write this code into key typed

    char c=evt.getKeyChar();
    if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE || c==KeyEvent.VK_DELETE)))
    {
        getToolkit().beep();
        evt.consume();
    }
    

提交回复
热议问题