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

  • 相关阅读:
    Shiro权限验证
    5种设计模式整理
    多模块的SpringBoot项目
    Go使用数据库
    使用Go mod
    docker基本使用
    Go的IO操作
    实现一个网盘存储……
    Go的网络编程
    学习golang的历程
  • 原文地址:https://www.cnblogs.com/bingrong/p/3302860.html
Copyright © 2011-2022 走看看