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

前端 未结 19 2118
陌清茗
陌清茗 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:01

    Try this out in the key pressed event for the related JTextField.

    private void JTextField(java.awt.event.KeyEvent evt) {
    
        // TODO add your handling code here:
        char enter = evt.getKeyChar();
        if(!(Character.isDigit(enter))){
            evt.consume();
        }
    }
    

提交回复
热议问题