Java 文本控件内容录入限制(含源代码说明)(二)

来源:互联网 发布:做淘宝创业家破人亡 编辑:程序博客网 时间:2024/04/30 01:08

<script type="text/javascript"><!--google_ad_client = "pub-4615277071069293";/* 728x15-横向B */google_ad_slot = "8797476329";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

博采众生本文介绍的源代码内容需要配合这篇文章的第一篇一起,点击文本控件内容录入限制(含源代码说明)(一)查看,所有的源代码可以在CSDN的下载空间找到,名字为:“文本录入限制源代码”,同时也可以留言(Email)我来发送给你,希望你在看过以后,留下建议。

  1. package net.csdn.blog.qb2049_xg.tools;
  2. import java.awt.Toolkit;
  3. import javax.swing.InputVerifier;
  4. import javax.swing.JComponent;
  5. import javax.swing.JOptionPane;
  6. import javax.swing.text.AttributeSet;
  7. import javax.swing.text.BadLocationException;
  8. import javax.swing.text.JTextComponent;
  9. import javax.swing.text.PlainDocument;
  10. /**
  11.  * @author Ulysses Ma
  12.  * @date 2008-9-10
  13.  * 参考以下网址Steve Cheng‘s Blog:
  14.  * http://blog.csdn.net/stevech/archive/2006/04/09/656269.aspx
  15.  */
  16. public class JTextHelp {
  17.     //功能控件
  18.     private JTextComponent jtc;
  19.     //实现各个功能常量的定义
  20.     public static final int NUMBER = 1;
  21.     public static final int STRICT_NUMBER=2;
  22.     public static final int LOWERCASE=3;
  23.     public static final int UPPERCASE=4;
  24.     public static final int IPv4CHECK=5;
  25.     public static final int MAX_MIN=6;
  26.     //全局功能变量
  27.     public int task=0;
  28.     public int max=0;
  29.     public int min=0;
  30.     //信息提示的全局变量
  31.     public String message=null;
  32.     
  33.     //是否需要提示信息的设定,默认值为:false(不需要)
  34.     private boolean needMessage=false;
  35.     //构造函数1,实现是否需要提示信息
  36.     public JTextHelp(JTextComponent jtc,int task,boolean needMessage){
  37.         if(task!=NUMBER&&task!=LOWERCASE&
  38.                    task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
  39.                     throw new IllegalArgumentException("JTextHelp的任务只能是:" +
  40.                             "NUMBER,STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
  41.                 }
  42.         this.jtc=jtc;
  43.         this.task=task;
  44.         this.needMessage=needMessage;
  45.     }
  46.     //构造函数2,实现不需要提示信息
  47.     public JTextHelp(JTextComponent jtc,int task){
  48.         if(task!=NUMBER&&task!=LOWERCASE&
  49.                    task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
  50.                     throw new IllegalArgumentException("JTextHelp的任务只能是:" +
  51.                             "NUMBER,STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
  52.                 }
  53.         this.jtc=jtc;
  54.         this.task=task;
  55.     }
  56.     //构造函数3,实现是否需要提示信息,数字范围限制
  57.     public JTextHelp(JTextComponent jtc,int task,boolean needMessage,int min,int max){
  58.         if(task!=NUMBER&&task!=LOWERCASE&
  59.                    task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
  60.                     throw new IllegalArgumentException("JTextHelp的任务只能是:" +
  61.                             "NUMBER,STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
  62.                 }
  63.         this.jtc=jtc;
  64.         this.task=task;
  65.         this.needMessage=needMessage;
  66.         this.min=min;
  67.         this.max=max;
  68.     }
  69.     //构造函数4,实现不需要提示信息,数字范围限制
  70.     public JTextHelp(JTextComponent jtc,int task,int min,int max){
  71.         if(task!=NUMBER&&task!=LOWERCASE&
  72.                    task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
  73.                     throw new IllegalArgumentException("JTextHelp的任务只能是:" +
  74.                             "NUMBER,STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
  75.                 }
  76.         this.jtc=jtc;
  77.         this.task=task;
  78.         this.min=min;
  79.         this.max=max;
  80.     }
  81.     //构造函数5,无参可以调用处理方法
  82.     public JTextHelp(){}
  83.     
  84.     //进行数据任务输入的检查
  85.     public void insertCheck(){
  86.         switch(task){
  87.             case NUMBER:{
  88.                 jtc.setDocument(new JNumber());
  89.             };break;
  90.             case STRICT_NUMBER:{
  91.                 jtc.setDocument(new JStrictNumber());
  92.             };break;
  93.             case UPPERCASE:{
  94.                 jtc.setDocument(new LetterCaseConvert(LetterCaseConvert.UPCASE));
  95.             };break;
  96.             case LOWERCASE:{
  97.                 jtc.setDocument(new LetterCaseConvert(LetterCaseConvert.LOWERCASE));
  98.             };break;
  99.             case IPv4CHECK:{
  100.                 jtc.setInputVerifier(new MyInputVerifier(JTextHelp.IPv4CHECK));
  101.             };break;
  102.             case MAX_MIN:{
  103.                 jtc.setInputVerifier(new MyInputVerifier(JTextHelp.MAX_MIN,min,max));
  104.             };break;
  105.         }
  106.     }
  107.     //严格数字输入不含的小数点和负号
  108.     class JStrictNumber extends PlainDocument{
  109.         //序列化标识符的设定
  110.         private static final long serialVersionUID = 2049L;
  111.         /*
  112.          * offs:代表了起始位置
  113.          * str:代表插入字符串
  114.          * a:插入内容的属性
  115.          */
  116.         //@exception BadLoactionException
  117.          public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
  118.              char source[]=str.toCharArray();
  119.              char result[]=new char[source.length];
  120.              message="错误的录入,系统仅接受数字的录入,请检查你的输入!";
  121.              int j=0;
  122.              for(int i=0;i<source.length;i++){
  123.                  if(Character.isDigit(source[i])){
  124.                      result[j++]=source[i]; 
  125.                  }else{
  126.                      if(needMessage){
  127.                          JOptionPane.showMessageDialog(null,message,
  128.                                  "信息提示",JOptionPane.ERROR_MESSAGE);
  129.                      }else
  130.                      {
  131.                          Toolkit.getDefaultToolkit().beep();
  132.                      }
  133.                      return;
  134.                 }
  135.              }
  136.              super.insertString(offs,new String(result,0,j), a);
  137.          }
  138.     }    
  139.     //普通的数字输入,含有小数和负号,负号必须放在第一位 ,小数点的个数只能是一个
  140.     class JNumber extends PlainDocument{
  141.         //序列化标识符的设定
  142.         private static final long serialVersionUID = 2049L;
  143.         //插入方法的重写
  144.         public void insertString(int offs,String str,AttributeSet a)throws BadLocationException{
  145.             char source[]=str.toCharArray();
  146.             char result[]=new char[source.length];
  147.             int j=0;
  148.             for(int i=0;i<source.length;i++){
  149.                 if((source[i]=='-')&&jtc.getCaretPosition()==0&&countMinus(jtc.getText())==0){
  150.                     result[j++]=source[i];
  151.                 }else if(Character.isDigit(source[i])){
  152.                     result[j]=source[i];
  153.                     j++;
  154.                 }else if(source[i]=='.'&&countDot(jtc.getText())==0){
  155.                     result[j++]=source[i];
  156.                 }else{
  157.                     if(needMessage){
  158.                          JOptionPane.showMessageDialog(null,"错误的录入,系统仅接受数字(含小数和负数)的录入," +
  159.                                 "请检查你的输入!","信息提示",JOptionPane.ERROR_MESSAGE);
  160.                      }else{
  161.                          Toolkit.getDefaultToolkit().beep();
  162.                      }
  163.                      return;
  164.                 }
  165.             }
  166.             super.insertString(offs, new String(result,0,j), a);
  167.         }
  168.     } 
  169.     //大小写输入的转换
  170.     class LetterCaseConvert extends PlainDocument{
  171.         //设定大写或是小写
  172.         private static final int UPCASE=1;
  173.         private static final int LOWERCASE=2;
  174.         //序列化标识符的设定
  175.         private static final long serialVersionUID = 2049L;
  176.         //设定功能变量
  177.         private int chan=0;
  178.         
  179.         public LetterCaseConvert(int chan){
  180.             this.chan=chan;
  181.         }
  182.         //插入方法的重写
  183.         public void insertString(int offs,String str,AttributeSet a) throws BadLocationException{
  184.             String result=null;
  185.             if(chan==LetterCaseConvert.UPCASE){
  186.                 result=str.toUpperCase();
  187.                 super.insertString(offs,result,a);
  188.             }else if(chan==LetterCaseConvert.LOWERCASE){
  189.                 result=str.toLowerCase();
  190.                 super.insertString(offs,result,a);
  191.             }           
  192.         }
  193.     }
  194.     /*
  195.      * 实现文本输入后的检查,输入整数的范围检查,输入IP地址正确性检查
  196.      */
  197.     class MyInputVerifier extends InputVerifier{
  198.         private JTextComponent jc;
  199.         private float task=0;
  200.         private float min,max;
  201.         public MyInputVerifier(int task){
  202.             this.task=task;
  203.         }
  204.         public MyInputVerifier(int task,float min,float max){
  205.             this.task=task;
  206.             this.max=max;
  207.             this.min=min;
  208.         }
  209.         public boolean verify(JComponent arg0) {
  210.             boolean rValue=false;
  211.             jc=(JTextComponent)arg0;
  212.             if(jc.getText().equals(""))
  213.                 return true;
  214.             if(task==JTextHelp.IPv4CHECK){
  215.                 rValue=checkIP(jc.getText());
  216.                 if(!rValue){
  217.                     JOptionPane.showMessageDialog(null,"输入的IP地址不合法,请检查你的输入!","信息提示",JOptionPane.ERROR_MESSAGE);
  218.                     return false;
  219.                 }
  220.             }
  221.             if(task==JTextHelp.MAX_MIN){
  222.                 try {
  223.                     int value=Integer.parseInt(jc.getText());
  224.                     if(value>=min&&value<=max)
  225.                          rValue=true;
  226.                     else{
  227.                         JOptionPane.showMessageDialog(null,"此处数据被限制在"+String.valueOf(this.min)+
  228.                                       "-"+String.valueOf(this.max)+"之间(含这两个数),请检查输入!","信息提示",JOptionPane.ERROR_MESSAGE);
  229.                     rValue=false;
  230.                     }
  231.                 } catch (NumberFormatException e) {
  232.                     JOptionPane.showMessageDialog(null,"错误的录入,请检查你的输入!"+e.getMessage(),"信息提示",JOptionPane.ERROR_MESSAGE);
  233.                     rValue=false;
  234.                 }
  235.             }
  236.             return rValue;
  237.         }       
  238.     }
  239.     
  240.     //计算字符串中小数点的个数
  241.     public int countDot(String str){
  242.         int count=0;
  243.         char strChar[]=str.toCharArray();
  244.         for(int i=0;i<strChar.length;i++)
  245.             if(strChar[i]=='.'){
  246.                 count++;
  247.             }
  248.         return count;
  249.     }   
  250.     //计算字符串中负号"-"的个数
  251.     public int countMinus(String str){
  252.         int count=0;
  253.         char strChar[]=str.toCharArray();
  254.         for(int i=0;i<strChar.length;i++)
  255.             if(strChar[i]=='-'){
  256.                 count++;
  257.             }
  258.         return count;
  259.     }
  260.     //检查IP地址是否合法
  261.     public boolean checkIP(String str){
  262.         int dot[]=new int[4];
  263.         String tmp1,tmp2,tmp3,tmp4;
  264.         /*初步判断是否为合法的IP字符串
  265.          * 开始和结束不能是".";
  266.          * 字符串中的'.'数量是3个;
  267.          * 字符串中的字符串长度必须小于16;
  268.          */
  269.         if(str.startsWith("."))
  270.             return false;
  271.         if(str.lastIndexOf(".")==(str.length()-1))      
  272.             return false;
  273.         if(str.length()>15)
  274.             return false;
  275.         if(countDot(str)!=3)
  276.             return false;
  277.         /*
  278.          * 字符的截取,然后进行正整数判断
  279.          */
  280.         for(int i=0;i<3;i++){
  281.             dot[i+1]=str.indexOf(".",dot[i]+1);
  282.         }
  283.         tmp1=str.substring(dot[0],dot[1]);
  284.         tmp2=str.substring(dot[1]+1,dot[2]);
  285.         tmp3=str.substring(dot[2]+1,dot[3]);
  286.         tmp4=str.substring(dot[3]+1,str.length());
  287.         try{
  288.             int a1=Integer.parseInt(tmp1);
  289.             int a2=Integer.parseInt(tmp2);
  290.             int a3=Integer.parseInt(tmp3);
  291.             int a4=Integer.parseInt(tmp4);
  292.             if(a1<0||a1>255||a2<0||a2>255||a3<0||a3>255||a4<0||a4>255)
  293.                 return false;
  294.             return true;
  295.         }catch(NumberFormatException nfe){
  296.             return false;
  297.         }
  298.     }
  299. }
  300. <script type="text/javascript"><!--google_ad_client = "pub-4615277071069293";/* 728x90, 创建于 08-6-3 */google_ad_slot = "8286133791";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

    <script type="text/javascript"><!--google_ad_client = "pub-4615277071069293";/* 图片广告-横向A */google_ad_slot = "5730752301";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

原创粉丝点击