zoukankan      html  css  js  c++  java
  • java中限制文本框输入长度的显示(转载)

    我最近在网上看到一篇文章很不错,对于限制文本框输入长度的显示很方便

    JAVA不像C#能够有maxLength这个属性对文本框的输入长度进行控制,但也是有办法实现相应的功能的。

    写一个MyDocument类,继承PlainDocument。重写insertString(int offset,String str,AttributeSet a)方法。

    方法如下:

    package com.dao;

    import javax.swing.text.BadLocationException;

    import javax.swing.text.PlainDocument;

    public class MyDocument extends PlainDocument {

     private int maxLength;      

    public MyDocument(int newMaxLength)      {    

          super();          maxLength=newMaxLength;      

    }      public MyDocument()      {      

        this(10);      }      

    public void insertString(int offset,String str,javax.swing.text.AttributeSet a) throws BadLocationException      {      

        if(getLength()+str.length()>maxLength)          {              return;          }       

       else          {              super.insertString(offset, str,a);          }      

     

     

     

    如果觉得不详细,可以参考http://blog.sina.com.cn/s/blog_7750745b0101a56h.html

  • 相关阅读:
    六大设计原则之依赖倒置原则
    六大设计原则之里氏替换原则
    六大设计原则之单一设计原则
    六、Spring之DI的Bean的作用域
    五、spring之DI循环依赖
    四、spring之DI
    十二 NIO和IO
    十一 Pipe
    十 DatagramChannel
    九 ServerSocketChannel
  • 原文地址:https://www.cnblogs.com/bingrong/p/3302860.html
Copyright © 2011-2022 走看看