【问题标题】:Control space,numbers,special charaters,String in JTextfield in Java SwingJava Swing中JTextfield中的控制空间、数字、特殊字符、字符串
【发布时间】:2011-01-10 09:22:24
【问题描述】:

我的 Swing 组件中有两个文本字段。在一个文本字段中,我只需要有数字 (没有字符串,空格,允许特殊字符)并且在另一个文本字段中我只需要字符串(没有数字,空格,允许特殊字符)。我该如何实现..???

【问题讨论】:

    标签: java swing


    【解决方案1】:

    您可以使用Pattern 类(正则表达式)来验证输入。一个简短的教程是可用的here

    我很确定基础教程涵盖了所有这些...

    "^//d+$" //The text must have at least one digit, no other characters are allowed
    "^[a-zA-Z]+$" //The text must have at least one letter, no other characters are allowed
    

    【讨论】:

    • 举个例子会更有趣...:)
    • 我已经修改了我的回复。它应该可以解决问题。我没有对它们进行严格的测试。
    • thnx npinti,我们应该给字符串 xyz.matches("^//d+$");对吗???
    • 只需使用我给您的模式创建一个模式,然后使用 .matches 方法。你应该能够在我给你的链接和在线教程中找到你需要的东西......
    • 感谢 npinti 帮了大忙...:)
    【解决方案2】:

    您有两种选择,您可以在 1) 输入时或 2) 当用户执行操作(例如单击确认按钮)时验证字段中的文本。

    对于 2) npinti 的回答应该引导您朝着正确的方向前进,只需获取该字段的值并使用正则表达式对其进行验证。

    对于 1),您可能想要编写一个 KeyListener 来拦截按键并只允许该字段使用正确类型的字符。

    【讨论】:

      【解决方案3】:

      您可以扩展 javax.swing.text.PlainDocument 类,并调用 setDocument 方法 textfield。这是一个例子;

      package textfield;
      import java.awt.Toolkit;
      import javax.swing.text.AttributeSet;
      import javax.swing.text.BadLocationException;
      import javax.swing.text.PlainDocument;
      
      
      public class LimitedValuePositiveIntegerDocument extends PlainDocument {
      
          int maxValue;
          int maxLength;
          Toolkit toolkit;
      
          /**
           * Constructor for the class.
           * @param max maximum value of the number
           */
          public LimitedValuePositiveIntegerDocument(int max){
              maxValue = max;
              maxLength = (""+max).length();
              toolkit = Toolkit.getDefaultToolkit();
          }
      
          /**
           * Inserts the input string to the current string after validation.
           * @param offs offset of the place where the input string is supposed to be inserted.
           * @param str input string to be inserted
           * @param a attribute set
           */
          @Override
          public void insertString(int offs, String str, AttributeSet a)
                  throws BadLocationException {
      
              if(str == null)return;
              String currentText = getText(0,getLength());
              String resultText = new String();
              int i;
              boolean errorFound = false;
              boolean deleteFirstZero = false;
              int accepted=0;
      
              for (i = 0; (i<str.length())&&(!errorFound); i++) {
                  if (Character.isDigit(str.charAt(i))) { /* if it is digit */
                      if (offs==currentText.length()) {   /* calculates the resultant string*/
                          resultText = currentText+str.substring(0,i+1);
                      } else if (offs==0) {
                          resultText = str.substring(0,i+1)+currentText;
                      } else {
                          resultText = currentText.substring(0, offs)+str.substring(0,i+1)+currentText.substring(offs,currentText.length());
                      }
                      if (Integer.parseInt(resultText) > maxValue) {
                          errorFound = true;
                          toolkit.beep();
                      } else {
                          if ( resultText.length() == maxLength+1) {
                              deleteFirstZero = true;
                          }
                          accepted++;
                      }
                  } else {
                      errorFound = true;
                      toolkit.beep();
                  }
              }
      
              if ( accepted>0 ) { /* insert string */
                  super.insertString(offs, str.substring(0,accepted), a);
                  if (deleteFirstZero) {
                      super.remove(0,1);
                  }
              }
          }
      
          /**
           * Removes a part of the current string.
           * @param offs offset of the place to be removed.
           * @param len length to be removed
           */
          @Override
          public void remove(int offs, int len) throws BadLocationException{
              super.remove(offs, len);
          }
      
          /**
           * Returns max value of the number.
           * @return max value
           */
          public int getMaxValue() {
              return maxValue;
          }
      
          /**
           * Sets max value of the number.
           * @param max maximum value of the number
           */
          public void setMaxValue(int max) {
              this.maxValue = max;
          }
      } // end of class
      

      编辑: 及其用法;

      LimitedValuePositiveIntegerDocument doc = new LimitedValuePositiveIntegerDocument(999);
      JTextField numberField = new JtextField();
      numberField.setDocument(doc);
      

      您只能输入小于1000的正数,并且在您按键时检查..

      【讨论】:

      • 我无法理解您的建议...:(
      猜你喜欢
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多