本文介绍的源代码内容需要 配合这篇文章的第一篇一起,点击文本控件内容录入限制(含源代码说明)(一)查看,所有的源代码可以在CSDN的下载空间找到,名字为:“ 文本录入限制源代码”,同时也可以留言(Email)我来发送给你,希望你在看过以后,留下建议。
- packagenet.csdn.blog.qb2049_xg.tools;
- importjava.awt.Toolkit;
- importjavax.swing.InputVerifier;
- importjavax.swing.JComponent;
- importjavax.swing.JOptionPane;
- importjavax.swing.text.AttributeSet;
- importjavax.swing.text.BadLocationException;
- importjavax.swing.text.JTextComponent;
- importjavax.swing.text.PlainDocument;
- /**
- *@authorUlyssesMa
- *@date2008-9-10
- *参考以下网址SteveCheng‘sBlog:
- *http://blog.csdn.net/stevech/archive/2006/04/09/656269.aspx
- */
- publicclassJTextHelp{
- //功能控件
- privateJTextComponentjtc;
- //实现各个功能常量的定义
- publicstaticfinalintNUMBER=1;
- publicstaticfinalintSTRICT_NUMBER=2;
- publicstaticfinalintLOWERCASE=3;
- publicstaticfinalintUPPERCASE=4;
- publicstaticfinalintIPv4CHECK=5;
- publicstaticfinalintMAX_MIN=6;
- //全局功能变量
- publicinttask=0;
- publicintmax=0;
- publicintmin=0;
- //信息提示的全局变量
- publicStringmessage=null;
- //是否需要提示信息的设定,默认值为:false(不需要)
- privatebooleanneedMessage=false;
- //构造函数1,实现是否需要提示信息
- publicJTextHelp (JTextComponentjtc,inttask,booleanneedMessage){
- if(task! =NUMBER&&task!=LOWERCASE&
- &nbs p;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN) {
- &nbs p;thrownewIllegalArgumentException("JTextHelp的任务只能 是:"+
- &nbs p;"NUMBER, STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
- }
- this.jtc=jtc;
- this.task=task;
- this.needMessage=needMessage;
- }
- //构造函数2,实现不需要提示信息
- publicJTextHelp (JTextComponentjtc,inttask){
- if (task!=NUMBER&&task!=LOWERCASE&
- &nbs p;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbs p;thrownewIllegalArgumentException("JTextHelp的任务只能 是:"+
- &nbs p;"NUMBER, STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
- }
- this.jtc=jtc;
- this.task=task;
- }
- //构造函数3,实现是否需要提示信息,数字范围限 制
- publicJTextHelp (JTextComponentjtc,inttask,booleanneedMessage,intmin,intmax){
- if (task!=NUMBER&&task!=LOWERCASE&
- &nbs p;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbs p;thrownewIllegalArgumentException("JTextHelp的任务只能 是:"+
- &nbs p;"NUMBER, STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
- }
- this.jtc=jtc;
- this.task=task;
- this.needMessage=needMessage;
- this.min=min;
- this.max=max;
- }
- //构造函数4,实现不需要提示信息,数字范围限制
- publicJTextHelp (JTextComponentjtc,inttask,intmin,intmax){
- if(task! =NUMBER&&task!=LOWERCASE&
- &nbs p;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN) {
- &nbs p;thrownewIllegalArgumentException("JTextHelp的任务只能 是:"+
- &nbs p;"NUMBER, STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
- }
- this.jtc=jtc;
- this.task=task;
- this.min=min;
- this.max=max;
- }
- //构造函数5,无参可以调用处理方法
- publicJTextHelp(){}
- //进行数据任务输入的检查
- publicvoidinsertCheck(){
- switch (task){
- caseNUMBER:{
- jtc. setDocument(newJNumber());
- };break;
- caseSTRICT_NUMBER:{
- jtc.setDoc ument(newJStrictNumber());
- };break;
- caseUPPERCASE:{
- jtc. setDocument(newLetterCaseConvert (LetterCaseConvert.UPCASE));
- };break;
- caseLOWERCASE:{
- jtc.setDoc ument(newLetterCaseConvert(LetterCaseConvert.LOWERCASE));
- };break;
- caseIPv4CHECK:{
- jtc. setInputVerifier(newMyInputVerifier(JTextHelp.IPv4CHECK));
- };break;
- caseMAX_MIN:{
- jtc.setInp utVerifier(newMyInputVerifier(JTextHelp.MAX_MIN,min,max));
- };break;
- }
- }
- //严格数字输入不含的小数点和负号
- classJStrictNumberextendsPlainDocument{
- //序列化标识符的设定
- privatestaticfinallongserialVersionUID=2049L;
- /*
- *offs:代表了起始位置
- *str:代表插入字符串
- *a:插入内容的属性
- */
- //@exceptionBadLoactionException
- publicvoidinsertString(intoffs,Stringstr,AttributeSeta)throwsBadLocationException{
- charsource[]=str.toCharArray();
- charresult[]=newchar[source.length];
- message="错误的录入,系统仅接受数字的录入,请检查你的输入!";
- intj=0;
- for(inti=0;i<source.length;i++){
- &nbs p;if(Character.isDigit(source[i])){
- &nbs p;result[j++]=source[i];
- &nbs p;}else{
- &nbs p;if(needMessage){
- &nbs p;JOptionPane.showMessageDialog(null,message,
- &nbs p;"信息提示",JOptionPane.ERROR_MESSAGE);
- &nbs p;}else
- &nbs p;{
- &nbs p;Toolkit.getDefaultToolkit().beep();
- &nbs p;}
- &nbs p;return;
- }
- }
- super.insertString(offs,newString (result,0,j),a);
- }
- }
- //普通的数字输入,含有小数和负号,负号必须放在 第一位,小数点的个数只能是一个
- classJNumberextendsPlainDocument{
- //序列化标识符的设定
- privatestaticfinallongserialVersionUID=2049L;
- //插入方法的重写
- publicvoidinsertString(intoffs,Stringstr,AttributeSeta)throwsBadLocationException{
- charsource[]=str.toCharArray();
- charresult[]=newchar[source.length];
- intj=0;
- for(inti=0;i<source.length;i++){
- if((source[i]=='-') &&jtc.getCaretPosition()==0&&countMinus(jtc.getText()) ==0){
- &nbs p;result[j++]=source[i];
- } elseif(Character.isDigit (source[i])){
- &nbs p;result[j]=source[i];
- &nbs p;j++;
- } elseif(source[i]=='.'&&countDot(jtc.getText())==0) {
- &nbs p;result[j++]=source[i];
- } else{
- &nbs p;if(needMessage){
- &nbs p;JOptionPane.showMessageDialog(null,"错误的录入,系统仅接受数字(含小数和负数)的录 入,"+
- &nbs p;"请检查你的输入!","信息提 示",JOptionPane.ERROR_MESSAGE);
- &nbs p;}else{
- &nbs p;Toolkit.getDefaultToolkit().beep();
- &nbs p;}
- &nbs p;return;
- }
- }
- super.insertString(offs,newString (result,0,j),a);
- }
- }
- //大小写输入的转换
- classLetterCaseConvertextendsPlainDocument{
- //设定大写或是小写
- privatestaticfinalintUPCASE=1;
- privatestaticfinalintLOWERCASE=2;
- //序列化标识符的设定
- privatestaticfinallongserialVersionUID=2049L;
- //设定功能变量
- privateintchan=0;
- publicLetterCaseConvert(intchan) {
- this.chan=chan;
- }
- //插入方法的重写
- publicvoidinsertString(intoffs,Stringstr,AttributeSeta)throwsBadLocationException{
- Stringresult=null;
- if(chan==LetterCaseConvert.UPCASE){
- result=str .toUpperCase();
- super.insertString(offs,result,a);
- }elseif(chan==LetterCaseConvert.LOWERCASE) {
- resu lt=str.toLowerCase();
- <sp an class="keyword">super<span>.insertString(offs,result,a);</span></sp>
- }
- }
- }
- /*
- *实现文本输入后的检查,输入整数的范围检查,输入 IP地址正确性检查
- */
- classMyInputVerifierextendsInputVerifier{
- privateJTextComponentjc;
- privatefloattask=0;
- privatefloatmin,max;
- publicMyInputVerifier(inttask) {
- this.task=task;
- }
- publicMyInputVerifier(inttask,floatmin,floatmax){
- this.task=task;
- this.max=max;
- this.min=min;
- }
- publicbooleanverify (JComponentarg0){
- booleanrValue=false;
- jc=(JTextComponent) arg0;
- if(jc.getText().equals(""))
- returntrue;
- if(task==JTextHelp.IPv4CHECK){
- rVal ue=checkIP(jc.getText());
- <sp an class="keyword">if<span>(!rValue){</span></sp>
- &nbs p;JOptionPane.showMessageDialog(null,"输入的IP地址不合法,请检查你的输入!","信息提 示",JOptionPane.ERROR_MESSAGE);
- &nbs p;returnfalse;
- }
- }
- if(task==JTextHelp.MAX_MIN){
- <sp an class="keyword">try<span>{</span></sp>
- &nbs p;intvalue=Integer.parseInt(jc.getText ());
- &nbs p;if(value>=min&&value<=max)
- &nbs p;rValue=true;
- &nbs p;else{
- &nbs p;JOptionPane.showMessageDialog(null,"此处数据被限制在"+String.valueOf (this.min)+
- &nbs p; "-"+String.valueOf(this.max) +"之间(含这两个数),请检查输入!","信息提 示",JOptionPane.ERROR_MESSAGE);
- &nbs p;rValue=false;
- &nbs p;}
- } catch(NumberFormatExceptione){
- &nbs p;JOptionPane.showMessageDialog(null," 错误的录入,请检查你的输入!"+e.getMessage(),"信息提 示",JOptionPane.ERROR_MESSAGE);
- &nbs p;rValue=false;
- }
- }
- returnrValue;
- }
- }
- //计算字符串中小数点的个数
- publicintcountDot(Stringstr){
- intcount=0;
- charstrChar[]=str.toCharArray();
- for(inti=0;i<strChar.length;i++)
- if(strChar[i]=='.'){
- count++; SPAN>
- }
- returncount;
- }
- //计算字符串中负号"-"的个数
- publicintcountMinus(Stringstr){
- intcount=0;
- charstrChar[]=str.toCharArray();
- for(inti=0;i<strChar.length;i++)
- if(strChar[i]=='-'){
- count++; SPAN>
- }
- returncount;
- }
- //检查IP地址是否合法
- publicbooleancheckIP(Stringstr){
- intdot[] =newint[4];
- Stringtmp1,tmp2,tmp3,tmp4;
- /*初步判断是否为合法的IP字符串
- *开始和结束不能 是".";
- *字符串中的'.'数量是3 个;
- *字符串中的字符串长度 必须小于16;
- */
- if (str.startsWith("."))
- returnfalse;
- if (str.lastIndexOf(".")==(str.length()-1))
- returnfalse;
- if (str.length()>15)
- returnfalse;
- if (countDot(str)!=3)
- returnfalse;
- /*
- *字符的截取,然后进行正整数判断
- */
- for(inti=0;i<3;i++){
- dot[i+1]=str.indexOf(".",dot[i]+1);
- }
- tmp1=str.substring(dot[0],dot[1]);
- tmp2=str.substring(dot[1]+1,dot[2]);
- tmp3=str.substring(dot[2]+1,dot[3]);
- tmp4=str.substring(dot[3]+1,str.length());
- try {
- inta1=Integer.parseInt(tmp1);
- inta2=Integer.parseInt(tmp2);
- inta3=Integer.parseInt(tmp3);
- inta4=Integer.parseInt(tmp4);
- if(a1<0||a1>255||a2<0||a2>255||a3<0||a3>255||a4<0||a4>255)
- returnfalse;
- returntrue;
- }catch (NumberFormatExceptionnfe){
- returnfalse;
- }
- }
- }