|
本文介绍的源代码内容需要配合这篇文章的第一篇一起,点击文本控件内容录入限制(含源代码说明)(一)查看,所有的源代码可以在CSDN的下载空间找到,名字为:“文本录入限制源代码”,同时也可以留言(Email)我来发送给你,希望你在看过以后,留下建议。
<span style="font-size: 16px; color: #000000;"><div class="highlighter">
- packagenet.csdn.blog.qb2049_xg.tools;<li class="alt">
- importjava.awt.Toolkit;<li class="alt">
- 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;<li class="alt">
- /**
- *@authorUlyssesMa
- *@date2008-9-10
- *参考以下网址SteveCheng‘sBlog:
- *http://blog.csdn.net/stevech/archive/2006/04/09/656269.aspx
- */
- publicclassJTextHelp{<li class="alt">
- //功能控件
- privateJTextComponentjtc;
- //实现各个功能常量的定义
- publicstaticfinalintNUMBER=1;
- publicstaticfinalintSTRICT_NUMBER=2;
- publicstaticfinalintLOWERCASE=3;
- publicstaticfinalintUPPERCASE=4;
- publicstaticfinalintIPv4CHECK=5;
- publicstaticfinalintMAX_MIN=6;
- //全局功能变量
- publicinttask=0;
- publicintmax=0;
- publicintmin=0;
- //信息提示的全局变量
- publicStringmessage=null;<li class="alt">
- //是否需要提示信息的设定,默认值为:false(不需要)
- privatebooleanneedMessage=false;
- //构造函数1,实现是否需要提示信息
- publicJTextHelp(JTextComponentjtc,inttask,booleanneedMessage){
- if(task!=NUMBER&&task!=LOWERCASE&
- &nbsp;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbsp;thrownewIllegalArgumentException("JTextHelp的任务只能是:"+
- &nbsp;"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&
- &nbsp;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbsp;thrownewIllegalArgumentException("JTextHelp的任务只能是:"+
- &nbsp;"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&
- &nbsp;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbsp;thrownewIllegalArgumentException("JTextHelp的任务只能是:"+
- &nbsp;"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&
- &nbsp;task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbsp;thrownewIllegalArgumentException("JTextHelp的任务只能是:"+
- &nbsp;"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.setDocument(newJStrictNumber());
- };break;
- caseUPPERCASE:{
- jtc.setDocument(newLetterCaseConvert(LetterCaseConvert.UPCASE));
- };break;
- caseLOWERCASE:{
- jtc.setDocument(newLetterCaseConvert(LetterCaseConvert.LOWERCASE));
- };break;
- caseIPv4CHECK:{
- jtc.setInputVerifier(newMyInputVerifier(JTextHelp.IPv4CHECK));
- };break;
- caseMAX_MIN:{
- jtc.setInputVerifier(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++){
- &nbsp;if(Character.isDigit(source)){
- &nbsp;result[j++]=source;
- &nbsp;}else{
- &nbsp;if(needMessage){
- &nbsp;JOptionPane.showMessageDialog(null,message,
- &nbsp;"信息提示",JOptionPane.ERROR_MESSAGE);
- &nbsp;}else
- &nbsp;{
- &nbsp;Toolkit.getDefaultToolkit().beep();
- &nbsp;}
- &nbsp;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=='-')&&jtc.getCaretPosition()==0&&countMinus(jtc.getText())==0){
- &nbsp;result[j++]=source;
- }elseif(Character.isDigit(source)){
- &nbsp;result[j]=source;
- &nbsp;j++;
- }elseif(source=='.'&&countDot(jtc.getText())==0){
- &nbsp;result[j++]=source;
- }else{
- &nbsp;if(needMessage){
- &nbsp;JOptionPane.showMessageDialog(null,"错误的录入,系统仅接受数字(含小数和负数)的录入,"+
- &nbsp;"请检查你的输入!","信息提示",JOptionPane.ERROR_MESSAGE);
- &nbsp;}else{
- &nbsp;Toolkit.getDefaultToolkit().beep();
- &nbsp;}
- &nbsp;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){
- result=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){
- rValue=checkIP(jc.getText());
- <sp an class="keyword">if<span>(!rValue){</span></sp>
- &nbsp;JOptionPane.showMessageDialog(null,"输入的IP地址不合法,请检查你的输入!","信息提示",JOptionPane.ERROR_MESSAGE);
- &nbsp;returnfalse;
- }
- }
- if(task==JTextHelp.MAX_MIN){
- <sp an class="keyword">try<span>{</span></sp>
- &nbsp;intvalue=Integer.parseInt(jc.getText());
- &nbsp;if(value>=min&&value<=max)
- &nbsp;rValue=true;
- &nbsp;else{
- &nbsp;JOptionPane.showMessageDialog(null,"此处数据被限制在"+String.valueOf(this.min)+
- &nbsp;"-"+String.valueOf(this.max)+"之间(含这两个数),请检查输入!","信息提示",JOptionPane.ERROR_MESSAGE);
- &nbsp;rValue=false;
- &nbsp;}
- }catch(NumberFormatExceptione){
- &nbsp;JOptionPane.showMessageDialog(null,"错误的录入,请检查你的输入!"+e.getMessage(),"信息提示",JOptionPane.ERROR_MESSAGE);
- &nbsp;rValue=false;
- }
- }
- returnrValue;
- }
- }<li class="alt">
- //计算字符串中小数点的个数
- publicintcountDot(Stringstr){
- intcount=0;
- charstrChar[]=str.toCharArray();
- for(inti=0;i<strChar.length;i++)
- if(strChar=='.'){
- count++;SPAN>
- }
- returncount;
- }
- //计算字符串中负号"-"的个数
- publicintcountMinus(Stringstr){
- intcount=0;
- charstrChar[]=str.toCharArray();
- for(inti=0;i<strChar.length;i++)
- if(strChar=='-'){
- 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+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;
- }
- }
- }
|
|