在Java中验证整数值的问题 [英] Problem with Validating the Integer Value in Java

查看:74
本文介绍了在Java中验证整数值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Eclipse Rcp ,我需要验证仅接受我使用代码的整数值的文本框

hi i am using Eclipse Rcp and i need to validate the textbox that only accept the integer value for that i have use the code

 txtCapacity.addKeyListener(new KeyAdapter() {
              public void keyPressed(KeyEvent EVT) {                     

                    if((EVT.character>='0' && EVT.character<='9')){
                          txtCapacity.setEditable(true);                    
                           txtCapacity.setEditable(true);

                      } else {
                          txtCapacity.setEditable(false);
                             System.out.println("enter only the numeric number");                                  
                      }
              }
      });

它可以验证,但是问题是我不能使用Backspace键用于删除号码。还请告诉我验证小数的想法。
预先感谢

It validates But the Problem with this one is that i cannot use the Backspace key for deleteting the number. and please tell me idea for Validating the decimal too. thanks in advance

推荐答案

在使用侦听器时,您可以清空文本字段,而不必将其设置为空白不可编辑。您可以执行以下操作,该代码段基于您的代码。

As you are using a listener, you can empty the text field, instead of making it not-editable. You can do something like this, the snippet is based upon your code.

txtCapacity.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent EVT) {                     
             if(!(EVT.character>='0' && EVT.character<='9')){
                    txtCapabity.setText(""); 
             }
        }
});

或者如果使用 JFormattedTextField 更好。我不确定您是否在SWT中拥有它,即使您不尝试寻找类似的东西。

Or better if you use JFormattedTextField. I'm not sure if you have that in SWT, even if you don't then try to look for the similar.

这篇关于在Java中验证整数值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆