<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
本文介绍的源代码内容需要 配合这篇文章的第一篇一起,点击文本控件内容录入限制(含源代码说明)(一)查看,所有的源代码可以在CSDN的下载空间找到,名字为:“ 文本录入限制源代码”,同时也可以留言(Email)我来发送给你,希望你在看过以后,留下建议。
- package net.csdn.blog.qb2049_xg.tools;
- import java.awt.Toolkit;
- import javax.swing.InputVerifier;
- import javax.swing.JComponent;
- import javax.swing.JOptionPane;
- import javax.swing.text.AttributeSet;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.JTextComponent;
- import javax.swing.text.PlainDocument;
- /**
- * @author Ulysses Ma
- * @date 2008-9-10
- * 参考以下网址Steve Cheng‘s Blog:
- * http://blog.csdn.net/stevech/archive/2006/04/09/656269.aspx
- */
- public class JTextHelp {
- //功能控件
- private JTextComponent jtc;
- //实现各个功能常量的定义
- public static final int NUMBER = 1;
- public static final int STRICT_NUMBER=2;
- public static final int LOWERCASE=3;
- public static final int UPPERCASE=4;
- public static final int IPv4CHECK=5;
- public static final int MAX_MIN=6;
- //全局功能变量
- public int task=0;
- public int max=0;
- public int min=0;
- //信息提示的全局变量
- public String message=null;
- //是否需要提示信息的设定,默认值为:false(不需要)
- private boolean needMessage=false;
- //构造函数1,实现是否需要提示信息
- public JTextHelp (JTextComponent jtc,int task,boolean needMessage){
- if(task! =NUMBER&&task!=LOWERCASE&
- &nbs p; task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN) {
- &nbs p; throw new IllegalArgumentException("JTextHelp的任务只能 是:" +
- &nbs p; "NUMBER, STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
- }
- this.jtc=jtc;
- this.task=task;
- this.needMessage=needMessage;
- }
- //构造函数2,实现不需要提示信息
- public JTextHelp (JTextComponent jtc,int task){
- if (task!=NUMBER&&task!=LOWERCASE&
- &nbs p; task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbs p; throw new IllegalArgumentException("JTextHelp的任务只能 是:" +
- &nbs p; "NUMBER, STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
- }
- this.jtc=jtc;
- this.task=task;
- }
- //构造函数3,实现是否需要提示信息,数字范围限 制
- public JTextHelp (JTextComponent jtc,int task,boolean needMessage,int min,int max){
- if (task!=NUMBER&&task!=LOWERCASE&
- &nbs p; task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN){
- &nbs p; throw new IllegalArgumentException("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,实现不需要提示信息,数字范围限制
- public JTextHelp (JTextComponent jtc,int task,int min,int max){
- if(task! =NUMBER&&task!=LOWERCASE&
- &nbs p; task!=UPPERCASE&&task!=IPv4CHECK&&task!=STRICT_NUMBER&&task!=MAX_MIN) {
- &nbs p; throw new IllegalArgumentException("JTextHelp的任务只能 是:" +
- &nbs p; "NUMBER, STRICT_NUMBER,IPv4CHECK,LOWERCASE,MAX_MIN或是UPPERCASE");
- }
- this.jtc=jtc;
- this.task=task;
- this.min=min;
- this.max=max;
- }
- //构造函数5,无参可以调用处理方法
- public JTextHelp(){}
- //进行数据任务输入的检查
- public void insertCheck(){
- switch (task){
- case NUMBER:{
- jtc. setDocument(new JNumber());
- };break;
- case STRICT_NUMBER:{
- jtc.setDoc ument(new JStrictNumber());
- };break;
- case UPPERCASE:{
- jtc. setDocument(new LetterCaseConvert (LetterCaseConvert.UPCASE));
- };break;
- case LOWERCASE:{
- jtc.setDoc ument(new LetterCaseConvert(LetterCaseConvert.LOWERCASE));
- };break;
- case IPv4CHECK:{
- jtc. setInputVerifier(new MyInputVerifier(JTextHelp.IPv4CHECK));
- };break;
- case MAX_MIN:{
- jtc.setInp utVerifier(new MyInputVerifier(JTextHelp.MAX_MIN,min,max));
- };break;
- }
- }
- //严格数字输入不含的小数点和负号
- class JStrictNumber extends PlainDocument{
- //序列化标识符的设定
- private static final long serialVersionUID = 2049L;
- /*
- * offs:代表了起始位置
- * str:代表插入字符串
- * a:插入内容的属性
- */
- //@exception BadLoactionException
- public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
- char source[]=str.toCharArray();
- char result[]=new char[source.length];
- message="错误的录入,系统仅接受数字的录入,请检查你的输入!";
- int j=0;
- for(int i=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,new String (result,0,j), a);
- }
- }
- //普通的数字输入,含有小数和负号,负号必须放在 第一位 ,小数点的个数只能是一个
- class JNumber extends PlainDocument{
- //序列化标识符的设定
- private static final long serialVersionUID = 2049L;
- //插入方法的重写
- public void insertString(int offs,String str,AttributeSet a)throws BadLocationException{
- char source[]=str.toCharArray();
- char result[]=new char[source.length];
- int j=0;
- for(int i=0;i<source.length;i++){
- if((source[i]=='-') &&jtc.getCaretPosition()==0&&countMinus(jtc.getText()) ==0){
- &nbs p; result[j++]=source[i];
- } else if(Character.isDigit (source[i])){
- &nbs p; result[j]=source[i];
- &nbs p; j++;
- } else if(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, new String (result,0,j), a);
- }
- }
- //大小写输入的转换
- class LetterCaseConvert extends PlainDocument{
- //设定大写或是小写
- private static final int UPCASE=1;
- private static final int LOWERCASE=2;
- //序列化标识符的设定
- private static final long serialVersionUID = 2049L;
- //设定功能变量
- private int chan=0;
- public LetterCaseConvert(int chan) {
- this.chan=chan;
- }
- //插入方法的重写
- public void insertString(int offs,String str,AttributeSet a) throws BadLocationException{
- String result=null;
- if(chan==LetterCaseConvert.UPCASE){
- result=str .toUpperCase();
- super.insertString(offs,result,a);
- }else if(chan==LetterCaseConvert.LOWERCASE) {
- resu lt=str.toLowerCase();
- super .insertString(offs,result,a);
- }
- }
- }
- /*
- * 实现文本输入后的检查,输入整数的范围检查,输入 IP地址正确性检查
- */
- class MyInputVerifier extends InputVerifier{
- private JTextComponent jc;
- private float task=0;
- private float min,max;
- public MyInputVerifier(int task) {
- this.task=task;
- }
- public MyInputVerifier(int task,float min,float max){
- this.task=task;
- this.max=max;
- this.min=min;
- }
- public boolean verify (JComponent arg0) {
- boolean rValue=false;
- jc=(JTextComponent) arg0;
- if(jc.getText().equals(""))
- return true;
- if(task==JTextHelp.IPv4CHECK){
- rVal ue=checkIP(jc.getText());
- if (!rValue){
- &nbs p; JOptionPane.showMessageDialog(null,"输入的IP地址不合法,请检查你的输入!","信息提 示",JOptionPane.ERROR_MESSAGE);
- &nbs p; return false;
- }
- }
- if(task==JTextHelp.MAX_MIN){
- try {
- &nbs p; int value=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 (NumberFormatException e) {
- &nbs p; JOptionPane.showMessageDialog(null," 错误的录入,请检查你的输入!"+e.getMessage(),"信息提 示",JOptionPane.ERROR_MESSAGE);
- &nbs p; rValue=false;
- }
- }
- return rValue;
- }
- }
- //计算字符串中小数点的个数
- public int countDot(String str){
- int count=0;
- char strChar[]=str.toCharArray();
- for(int i=0;i<strChar.length;i++)
- if(strChar[i]=='.'){
- count++;
- }
- return count;
- }
- //计算字符串中负号"-"的个数
- public int countMinus(String str){
- int count=0;
- char strChar[]=str.toCharArray();
- for(int i=0;i<strChar.length;i++)
- if(strChar[i]=='-'){
- count++;
- }
- return count;
- }
- //检查IP地址是否合法
- public boolean checkIP(String str){
- int dot[] =new int[4];
- String tmp1,tmp2,tmp3,tmp4;
- /*初步判断是否为合法的IP字符串
- * 开始和结束不能 是".";
- * 字符串中的'.'数量是3 个;
- * 字符串中的字符串长度 必须小于16;
- */
- if (str.startsWith("."))
- return false;
- if (str.lastIndexOf(".")==(str.length()-1))
- return false;
- if (str.length()>15)
- return false;
- if (countDot(str)!=3)
- return false;
- /*
- * 字符的截取,然后进行正整数判断
- */
- for(int i=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 {
- int a1=Integer.parseInt(tmp1);
- int a2=Integer.parseInt(tmp2);
- int a3=Integer.parseInt(tmp3);
- int a4=Integer.parseInt(tmp4);
- if(a1<0||a1>255||a2<0||a2>255||a3<0||a3>255||a4<0||a4>255)
- return false;
- return true;
- }catch (NumberFormatException nfe){
- return false;
- }
- }
- }
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>