• <legend id='Vrb9m'><style id='Vrb9m'><dir id='Vrb9m'><q id='Vrb9m'></q></dir></style></legend>
  • <tfoot id='Vrb9m'></tfoot>

    <small id='Vrb9m'></small><noframes id='Vrb9m'>

      <bdo id='Vrb9m'></bdo><ul id='Vrb9m'></ul>
      <i id='Vrb9m'><tr id='Vrb9m'><dt id='Vrb9m'><q id='Vrb9m'><span id='Vrb9m'><b id='Vrb9m'><form id='Vrb9m'><ins id='Vrb9m'></ins><ul id='Vrb9m'></ul><sub id='Vrb9m'></sub></form><legend id='Vrb9m'></legend><bdo id='Vrb9m'><pre id='Vrb9m'><center id='Vrb9m'></center></pre></bdo></b><th id='Vrb9m'></th></span></q></dt></tr></i><div id='Vrb9m'><tfoot id='Vrb9m'></tfoot><dl id='Vrb9m'><fieldset id='Vrb9m'></fieldset></dl></div>

      1. 如何使用输入长度和范围设置 DocumentFilter?例如1-3 或 10-80

        How to set DocumentFilter with input length and range? e.g. 1-3 or 10-80(如何使用输入长度和范围设置 DocumentFilter?例如1-3 或 10-80)
          <tbody id='vnZ4K'></tbody>

        <tfoot id='vnZ4K'></tfoot>

        <small id='vnZ4K'></small><noframes id='vnZ4K'>

          <legend id='vnZ4K'><style id='vnZ4K'><dir id='vnZ4K'><q id='vnZ4K'></q></dir></style></legend>
          • <bdo id='vnZ4K'></bdo><ul id='vnZ4K'></ul>

              <i id='vnZ4K'><tr id='vnZ4K'><dt id='vnZ4K'><q id='vnZ4K'><span id='vnZ4K'><b id='vnZ4K'><form id='vnZ4K'><ins id='vnZ4K'></ins><ul id='vnZ4K'></ul><sub id='vnZ4K'></sub></form><legend id='vnZ4K'></legend><bdo id='vnZ4K'><pre id='vnZ4K'><center id='vnZ4K'></center></pre></bdo></b><th id='vnZ4K'></th></span></q></dt></tr></i><div id='vnZ4K'><tfoot id='vnZ4K'></tfoot><dl id='vnZ4K'><fieldset id='vnZ4K'></fieldset></dl></div>
                  本文介绍了如何使用输入长度和范围设置 DocumentFilter?例如1-3 或 10-80的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 DocumentFilter 将输入限制为整数或小数.我在这里发布的代码对此非常有效.

                  I'm using DocumentFilter to restrict input as integer or decimal. And the code I post here is working well for that.

                  谁能帮助我了解如何限制给定代码中的输入长度或范围?

                  Can anybody help me about how to restrict the input length or range in the given code?

                  谢谢!!

                  class MyIntFilter extends DocumentFilter {
                  public void insertString(FilterBypass fb, int offset, String string,
                       AttributeSet attr) throws BadLocationException {
                  
                    Document doc = fb.getDocument();
                    StringBuilder sb = new StringBuilder();
                  
                    sb.append(doc.getText(0, doc.getLength()));
                    sb.insert(offset, string);
                  
                    if (test(sb.toString())) {
                       super.insertString(fb, offset, string, attr);
                    } else {
                       // warn the user and don't allow the insert
                    }
                  }
                  
                  private boolean test(String text) {
                    try {
                       Integer.parseInt(text);
                       return true;
                    } catch (NumberFormatException e) {
                       return false;
                    }
                  }
                  
                  @Override
                  public void replace(FilterBypass fb, int offset, int length, String text,
                       AttributeSet attrs) throws BadLocationException {
                  
                    Document doc = fb.getDocument();
                    StringBuilder sb = new StringBuilder(2);
                    sb.append(doc.getText(0, doc.getLength()));
                    sb.replace(offset, offset + length, text);
                  
                    if (test(sb.toString())) {
                       super.replace(fb, offset, length, text, attrs);
                    } else {
                       // warn the user and don't allow the insert
                    }
                  
                  }
                  
                   @Override
                   public void remove(FilterBypass fb, int offset, int length)
                       throws BadLocationException {
                    Document doc = fb.getDocument();
                    StringBuilder sb = new StringBuilder(2);
                    sb.append(doc.getText(0, doc.getLength()));
                    //sb.append(doc.getText(0, 2));
                    sb.delete(offset, offset + length);
                  
                    if (test(sb.toString())) {
                       super.remove(fb, offset, length);
                    } else {
                       // warn the user and don't allow the insert
                    }
                  
                  }
                  
                  
                  }
                  

                  推荐答案

                  你可能想测试一下(因为我没有),但基本的想法应该让你开始.

                  You may want to test this (as I haven't), but the basic idea should get you started.

                  还可以查看 文档过滤器示例

                  至于设置最小长度,您可能需要使用 InputVerifier 以及

                  As to setting a minimum length, you may want to use a InputVerifier as well

                  class MyIntFilter extends DocumentFilter {
                  
                      private int maxLength = 0;
                  
                      public void setMaxLength(int maxLength) {
                          this.maxLength = maxLength;
                      }
                  
                      public int getMaxLength() {
                          return maxLength;
                      }
                  
                      public void insertString(FilterBypass fb, int offset, String string,
                                      AttributeSet attr) throws BadLocationException {
                  
                          Document doc = fb.getDocument();
                          StringBuilder sb = new StringBuilder();
                  
                          sb.append(doc.getText(0, doc.getLength()));
                          sb.insert(offset, string);
                  
                          if (maxLength > 0 && doc.getLength() + string.length() <= maxLength) {
                              if (test(sb.toString())) {
                                  super.insertString(fb, offset, string, attr);
                              } else {
                                  // warn the user and don't allow the insert
                              }
                          }
                      }
                  
                      private boolean test(String text) {
                          try {
                              Integer.parseInt(text);
                              return true;
                          } catch (NumberFormatException e) {
                              return false;
                          }
                      }
                  
                      @Override
                      public void replace(FilterBypass fb, int offset, int length, String text,
                                      AttributeSet attrs) throws BadLocationException {
                  
                          Document doc = fb.getDocument();
                          StringBuilder sb = new StringBuilder(2);
                          sb.append(doc.getText(0, doc.getLength()));
                          sb.replace(offset, offset + length, text);
                  
                          if (test(sb.toString())) {
                              if (sb.length() > maxLength) {
                                  length = sb.length() - maxLength;
                                  if (length > 0) {
                                      text = text.substring(0, length);
                                      super.replace(fb, offset, length, text, attrs);
                                  }
                              }
                          } else {
                              // warn the user and don't allow the insert
                          }
                  
                      }
                  
                      @Override
                      public void remove(FilterBypass fb, int offset, int length)
                                      throws BadLocationException {
                          Document doc = fb.getDocument();
                          StringBuilder sb = new StringBuilder(2);
                          sb.append(doc.getText(0, doc.getLength()));
                          //sb.append(doc.getText(0, 2));
                          sb.delete(offset, offset + length);
                  
                          if (test(sb.toString())) {
                              super.remove(fb, offset, length);
                          } else {
                              // warn the user and don't allow the insert
                          }
                  
                      }
                  }
                  

                  这篇关于如何使用输入长度和范围设置 DocumentFilter?例如1-3 或 10-80的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                      <tbody id='SBTAA'></tbody>
                    <tfoot id='SBTAA'></tfoot>
                    • <small id='SBTAA'></small><noframes id='SBTAA'>

                      1. <legend id='SBTAA'><style id='SBTAA'><dir id='SBTAA'><q id='SBTAA'></q></dir></style></legend>

                            <bdo id='SBTAA'></bdo><ul id='SBTAA'></ul>
                            <i id='SBTAA'><tr id='SBTAA'><dt id='SBTAA'><q id='SBTAA'><span id='SBTAA'><b id='SBTAA'><form id='SBTAA'><ins id='SBTAA'></ins><ul id='SBTAA'></ul><sub id='SBTAA'></sub></form><legend id='SBTAA'></legend><bdo id='SBTAA'><pre id='SBTAA'><center id='SBTAA'></center></pre></bdo></b><th id='SBTAA'></th></span></q></dt></tr></i><div id='SBTAA'><tfoot id='SBTAA'></tfoot><dl id='SBTAA'><fieldset id='SBTAA'></fieldset></dl></div>