如何限制输入到文本框? [英] How to restrict input to textboxes?
问题描述
大家好.我正在通过文本框获取数据,但我想限制它.即,用户不能输入字符串或任何其他内容.我只希望数字字符在100到999之间.可以吗?
String coursecode = Integer.parseInt(txtCourseCode.getText()); if(coursecode<100 || coursecode>999) { JOptionPane.showMessageDialog(null,"Incorrect Course Code"); }
这是我代码的一小部分.
Thanx.
验证是关键字.
无论您是创建自己的还是使用所提供的机制,都只需要在继续进行之前进行验证即可:
public class CustomVerifier 扩展 InputVerifier { @ Override 公共 布尔 verify(JComponent输入){ 字符串 text =(((JTextField)input).getText(); // 验证 返回 true; // 如果可以 // 返回false; //不好的时候 } }
并添加到JTextfield:
oTextField.setInputVerifier(新 CustomVerifier());
您还可以使用侦听器(propertyChange,focus)并在那里进行验证.
我需要提及的是您需要为此创建一个自定义JDialog吗?我猜不是.玩得开心.
具有侦听器的版本1:
textField.addKeyListener(新 KeyAdapter(){ // 适配器是侦听器的小版本", // 它仅实现所需的功能 @ Override 公共 无效 keyReleased(KeyEvent oEvent){ // 不喜欢该事件,我们只需要它有机会做出反应即可. 字符串 strContent = textField.getText(); 尝试 { int i = Integer.parseInt(strContent); // 通过简单解析进行检查 如果(i< = 0 || i> = 1000){// " ); } // 一切正常 } 捕获(NumberFormatException oException){ JOptionPane.showMessageDialog(null," ); } }); } }
带有InputVerifyer的Version2:
textField.setInputVerifier(新 InputVerifier(){ @ Override 公共 布尔 verify(JComponent输入){ 字符串 strContent = textField.getText(); 尝试 { int i = Integer.parseInt(strContent); // 通过简单解析进行检查 如果(i< = 0 || i> = 1000){// " ); } // 一切正常 } 捕获(NumberFormatException oException){ JOptionPane.showMessageDialog(null," ); } 返回 true; } });
希望Edit2有所帮助.您需要学习!
请购买一本书以获取此类信息,而无需知道关键字.
认为这确实可以解决.您可以尝试使用正则表达式将输入限制为仅数字,一旦完成,您就可以对值进行边界检查,这显然已经在代码中完成了.
>
Hello everyone. I am getting data via textbox but I want to restrict it. Namely, User can not enter character string or anything. I just want numeric characters between 100 and 999. Is it possible ?
String coursecode = Integer.parseInt(txtCourseCode.getText()); if(coursecode<100 || coursecode>999) { JOptionPane.showMessageDialog(null,"Incorrect Course Code"); }
this is a little part of my code.
Thanx.
Validation is the keyword.
No matter if you create your own or use a mechanism that is provided, you just need to validate before you move on:
public class CustomVerifier extends InputVerifier { @Override public boolean verify(JComponent input) { String text = ((JTextField) input).getText(); // verify return true; // if ok // return false; // when not ok } }
and add to the JTextfield:
oTextField.setInputVerifier(new CustomVerifier());
You can also use a listener (propertyChange, focus) and validate there.
EDIT:
Do I need to mention that you need to make a custom JDialog for this? I guess not. Have fun.
EDIT 2:
Version 1 with a Listener:
textField.addKeyListener(new KeyAdapter() { // Adapter is a "small version of a Listener", // which does implement just the needed functions @Override public void keyReleased(KeyEvent oEvent) { // disgard the Event, we just need it to have a chance to react. String strContent = textField.getText(); try { int i = Integer.parseInt(strContent); // check by simple parsing if(i<=0 || i>=1000) { // number between 1 and JOptionPane.showMessageDialog(null, "This number is <1 or >999."); } // everything is ok } catch (NumberFormatException oException) { JOptionPane.showMessageDialog(null,"This is not even a number."); } }); } }
Version2 with InputVerifyer:
textField.setInputVerifier(new InputVerifier() { @Override public boolean verify(JComponent input) { String strContent = textField.getText(); try { int i = Integer.parseInt(strContent); // check by simple parsing if(i<=0 || i>=1000) { // number between 1 and JOptionPane.showMessageDialog(null, "This number is <1 or >999."); } // everything is ok } catch (NumberFormatException oException) { JOptionPane.showMessageDialog(null, "This is not even a number."); } return true; } });
I hope Edit2 helps. You need to learn that!
Please buy a book to get such information without knowing the keywords.
Think this does count as a solution. You can try using Regular Expressions to restrict the input to numbers only and once that''s done you can have a bounds check on the values, which you already have done in your code apparently.
这篇关于如何限制输入到文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!