需要自己实现一个Document,
JTextField.setDocument();
例(可能复杂了点):import javax.swing.text.*;
import java.util.Arrays;
public class FilterDocument extends PlainDocument{ /**
* 用户自定义不允许输入类型的掩码
*/
public final static int USER_DEFINE = 1; /**
* 不允许输入数字的掩码
*/
public final static int NUMBER = 2; /**
* 不允许输入小写字母的掩码
*/
public final static int LOWCASE_ASCII = 4; /**
* 不允许输入大写字母的掩码
*/
public final static int UPCASE_ASCII = 8; /**
* 不允许输入字母的掩码
*/
public final static int ASCII = LOWCASE_ASCII + UPCASE_ASCII; /**
* 不允许输入字母和数字的掩码
*/
public final static int ASCII_AND_NUMBER = LOWCASE_ASCII + UPCASE_ASCII + NUMBER; /**
* 不允许输入GB2312范围的汉字
*/
public final static int GB2312 = 16; /**
* 数字的ASCII码起始位置
*/
private int[] number = {'0','9'}; /**
* 小写字母的ASCII码起始位置
*/
private int[] lowcaseAscii = {'a','z'}; /**
* 大写字母的ASCII码起始位置
*/
private int[] upcaseAscii = {'A','Z'};
private int[] gb2312 = {0x8140,0xF7FE};
/**
* 用户自定义范围字符的的ASCII码起始位置数组(可能会有多段)
*/
private int[][] userDefine = null; /**
* 当前不允许输入类型
*/
private int maskType = 0; /**
* 构造方法
* 缺省只允许输入数字和字母
*/
public FilterDocument(){
this(ASCII_AND_NUMBER);
} /**
* 构造方法
* @param pMaskType 需要限制的类型掩码
* @param pMask 不允许该字串包含范围内的字符输入
* @see #USER_DEFINE
* @see #NUMBER
* @see #LOWCASE_ASCII
* @see #UPCASE_ASCII
*/
public FilterDocument(int pMaskType, String pMask){
this(pMaskType + USER_DEFINE);
parse(pMask);
} /**
* 构造方法
* @param pMask 不允许该字串包含范围内的字符输入
*/
public FilterDocument(String pMask){
this(0,pMask);
} /**
* 构造方法
* @param pMaskType 需要限制的类型掩码
* @see #USER_DEFINE
* @see #NUMBER
* @see #LOWCASE_ASCII
* @see #UPCASE_ASCII
*/
public FilterDocument(int pMaskType) {
maskType = pMaskType;
} /**
* 将字符串解析为字符起始范围数组
* @param pMask 包含用户不允许输入的字符的字符串
*/
private void parse(String pMask){
if(pMask == null || pMask.trim().length() == 0){
maskType &= (~USER_DEFINE);
} else {
char[] buf = pMask.toCharArray();
Arrays.sort(buf);
int i = 1;
int count = 0;
int chr = buf[0];
int end = -1;
int[][] userdefine = new int [buf.length][2];
userdefine[count][0] = chr;
while(i < buf.length){
chr = buf[i-1];
end = buf[i];
if(chr != end && chr != end -1){
userdefine[count++][1] = chr;
userdefine[count][0] = end;
}
i++;
}
if(end == -1){
userdefine[count++][1] = chr;
} else {
userdefine[count++][1] = end;
}
userDefine = new int[count][2];
System.arraycopy(userdefine,0,userDefine,0,count);
// 以下代码仅为测试使用
// for(int k = 0; k < userDefine.length; k++){
// System.out.println("TP:FormatDocument.parse: start = " + userDefine[k][0] + " end = " + userDefine[k][1] );
// }
} } /**
* 覆盖的PlainDocument的同名方法
* @param offset 偏移
* @param s 待插字串
* @param attributeSet 字串属性集合
* @throws BadLocationException 无效位置错误
*/
public void insertString(int offset,String s,AttributeSet attributeSet)throws BadLocationException {
char[] se = s.toCharArray();
char[] buf = new char[se.length]; int count = 0;
for(int i = 0; i < se.length; i++){ char chr = se[i];
// System.out.print(" " + (int)chr);
if((maskType & NUMBER) == NUMBER){
if(chr >= number[0] && chr <= number[1]){
continue;
}
}
if((maskType & LOWCASE_ASCII) == LOWCASE_ASCII){
if(chr >= lowcaseAscii[0] && chr <= lowcaseAscii[1]){
continue;
}
}
if((maskType & UPCASE_ASCII) == UPCASE_ASCII){
if(chr >= upcaseAscii[0] && chr <= upcaseAscii[1]){
continue;
}
}
if((maskType & GB2312) == GB2312){
if(chr >= gb2312[0] && chr <= gb2312[1]){
continue;
}
}
if((maskType & USER_DEFINE) == USER_DEFINE){
boolean found = false;
user:
for(int j = 0; j < userDefine.length; j++){
if(chr >= userDefine[j][0] && chr <= userDefine[j][1]){
found = true;
break user;
}
} // end for(j)
if(found)continue;
} // end if USER_DEFINE
buf[count ++] = chr;
}// end for(i)
if(count > 0){
s = new String(buf);
super.insertString (offset,s,attributeSet);
}
} /**
* 测试主入口
* @param argv
*/
public static void main(String[] argv){
ClsFormatDocument doc = new ClsFormatDocument("*");
}}
JTextField.setDocument();
例(可能复杂了点):import javax.swing.text.*;
import java.util.Arrays;
public class FilterDocument extends PlainDocument{ /**
* 用户自定义不允许输入类型的掩码
*/
public final static int USER_DEFINE = 1; /**
* 不允许输入数字的掩码
*/
public final static int NUMBER = 2; /**
* 不允许输入小写字母的掩码
*/
public final static int LOWCASE_ASCII = 4; /**
* 不允许输入大写字母的掩码
*/
public final static int UPCASE_ASCII = 8; /**
* 不允许输入字母的掩码
*/
public final static int ASCII = LOWCASE_ASCII + UPCASE_ASCII; /**
* 不允许输入字母和数字的掩码
*/
public final static int ASCII_AND_NUMBER = LOWCASE_ASCII + UPCASE_ASCII + NUMBER; /**
* 不允许输入GB2312范围的汉字
*/
public final static int GB2312 = 16; /**
* 数字的ASCII码起始位置
*/
private int[] number = {'0','9'}; /**
* 小写字母的ASCII码起始位置
*/
private int[] lowcaseAscii = {'a','z'}; /**
* 大写字母的ASCII码起始位置
*/
private int[] upcaseAscii = {'A','Z'};
private int[] gb2312 = {0x8140,0xF7FE};
/**
* 用户自定义范围字符的的ASCII码起始位置数组(可能会有多段)
*/
private int[][] userDefine = null; /**
* 当前不允许输入类型
*/
private int maskType = 0; /**
* 构造方法
* 缺省只允许输入数字和字母
*/
public FilterDocument(){
this(ASCII_AND_NUMBER);
} /**
* 构造方法
* @param pMaskType 需要限制的类型掩码
* @param pMask 不允许该字串包含范围内的字符输入
* @see #USER_DEFINE
* @see #NUMBER
* @see #LOWCASE_ASCII
* @see #UPCASE_ASCII
*/
public FilterDocument(int pMaskType, String pMask){
this(pMaskType + USER_DEFINE);
parse(pMask);
} /**
* 构造方法
* @param pMask 不允许该字串包含范围内的字符输入
*/
public FilterDocument(String pMask){
this(0,pMask);
} /**
* 构造方法
* @param pMaskType 需要限制的类型掩码
* @see #USER_DEFINE
* @see #NUMBER
* @see #LOWCASE_ASCII
* @see #UPCASE_ASCII
*/
public FilterDocument(int pMaskType) {
maskType = pMaskType;
} /**
* 将字符串解析为字符起始范围数组
* @param pMask 包含用户不允许输入的字符的字符串
*/
private void parse(String pMask){
if(pMask == null || pMask.trim().length() == 0){
maskType &= (~USER_DEFINE);
} else {
char[] buf = pMask.toCharArray();
Arrays.sort(buf);
int i = 1;
int count = 0;
int chr = buf[0];
int end = -1;
int[][] userdefine = new int [buf.length][2];
userdefine[count][0] = chr;
while(i < buf.length){
chr = buf[i-1];
end = buf[i];
if(chr != end && chr != end -1){
userdefine[count++][1] = chr;
userdefine[count][0] = end;
}
i++;
}
if(end == -1){
userdefine[count++][1] = chr;
} else {
userdefine[count++][1] = end;
}
userDefine = new int[count][2];
System.arraycopy(userdefine,0,userDefine,0,count);
// 以下代码仅为测试使用
// for(int k = 0; k < userDefine.length; k++){
// System.out.println("TP:FormatDocument.parse: start = " + userDefine[k][0] + " end = " + userDefine[k][1] );
// }
} } /**
* 覆盖的PlainDocument的同名方法
* @param offset 偏移
* @param s 待插字串
* @param attributeSet 字串属性集合
* @throws BadLocationException 无效位置错误
*/
public void insertString(int offset,String s,AttributeSet attributeSet)throws BadLocationException {
char[] se = s.toCharArray();
char[] buf = new char[se.length]; int count = 0;
for(int i = 0; i < se.length; i++){ char chr = se[i];
// System.out.print(" " + (int)chr);
if((maskType & NUMBER) == NUMBER){
if(chr >= number[0] && chr <= number[1]){
continue;
}
}
if((maskType & LOWCASE_ASCII) == LOWCASE_ASCII){
if(chr >= lowcaseAscii[0] && chr <= lowcaseAscii[1]){
continue;
}
}
if((maskType & UPCASE_ASCII) == UPCASE_ASCII){
if(chr >= upcaseAscii[0] && chr <= upcaseAscii[1]){
continue;
}
}
if((maskType & GB2312) == GB2312){
if(chr >= gb2312[0] && chr <= gb2312[1]){
continue;
}
}
if((maskType & USER_DEFINE) == USER_DEFINE){
boolean found = false;
user:
for(int j = 0; j < userDefine.length; j++){
if(chr >= userDefine[j][0] && chr <= userDefine[j][1]){
found = true;
break user;
}
} // end for(j)
if(found)continue;
} // end if USER_DEFINE
buf[count ++] = chr;
}// end for(i)
if(count > 0){
s = new String(buf);
super.insertString (offset,s,attributeSet);
}
} /**
* 测试主入口
* @param argv
*/
public static void main(String[] argv){
ClsFormatDocument doc = new ClsFormatDocument("*");
}}
解决方案 »
- java String 判空
- 高手来看下代码,解决客户端和服务器互相通话的功能!
- 关于setExecuteBatch的一个简单问题
- java 如何取两个括号中间的值
- 照书上原样写的怎么会出错呢?
- 高分求助:哪里有聊天室的源代码??送分!!!
- 我想问下怎样修改系统时间啊
- 做一个多语言版本的网站该怎么做
- 菜鸟提问:如何用java.exe去运行不同路径下的class文件?
- 请教一个关于Java Application中Frame的模态非模态的问题。
- 在java中怎么取当前应用程序的启动路径?
- java.util.NoSuchElementException: Hashtable Enumerator是什么意思啊?
http://expert.csdn.net/Expert/topic/1783/1783604.xml?temp=.2353784
中我的回答。如果你的不充许输入日文是指“就是不能输入日文,但能输入汉字之类的”,那恐怕办不到。
如果只是不能输入占两个字符的字,就可以用getByte来判断,如果getByte的值是负值,则为中文之类的。
就行了
MaskFormatter fmt = null;
try {
fmt = new MaskFormatter("###.###.###.###");
} catch (java.text.ParseException e) {
}
JFormattedTextField tft1 = new JFormattedTextField(fmt);
那么还要考虑到IP地址
最多只能是255的情况
最前面的一位不能大于2
而后面两位的值还要考虑到
如果第一位是2那么第二位不能大于5
如果第二位等于5,那么第三位不能大于5
等等等等情况,这些可不是这种Format的强项
如果说要考虑很多情况的话,那么可以跟上面几位说的自己实现一个Document,这样来做吧
然后自己判断输入情况
就是如果是空掉的ip
就用0来填充
MaskFormatter fmt = null;
try {
fmt = new MaskFormatter("###.###.###.###");
} catch (java.text.ParseException e) {
}
fmt.setPlaceholder( "000.000.000.000" );
JFormattedTextField tft1 = new JFormattedTextField(fmt);