jTextField validation for numbers and one decimal point?

前端 未结 5 1395
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 02:52

I need to set a jTextField to accept only numbers and one decimal point.Nothing else. Decimal point can\'t be typed more than once and no other characters are allowed. How c

5条回答
  •  旧巷少年郎
    2020-12-20 03:42

    char c=evt.getKeyChar();
    if(!(Character.isDigit(c)||
        (c==KeyEvent.VK_BACK_SPACE)||c==KeyEvent.VK_DELETE||evt.getKeyChar() == '.')){
    //  evt.getKeyChar() == '.' does accept point when jtextfield accepts decimal number
      evt.consume();
      getToolkit().beep();
    }
    

提交回复
热议问题