package combrowser; import javaawtToolkit; import javaioFile; import javautilStringTokenizer; import javaxswingJTextField; import javaxswingtextAttributeSet; import javaxswingtextBadLocationException; import javaxswingtextDocument; import javaxswingtextPlainDocument; /** * 只能输入文件名的文本框 * @author dl */ public class JFileNameTextField extends javaxswingJTextField{ public static void main(String[] args) { // TODO Autogenerated method stub //测试代码 /*JFrame frame = new JFrame(文本框的内容限制测试); framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); frameadd(new JFileNameTextField(true) BorderLayoutCENTER); frameadd(new JNumTextField( ) BorderLayoutNORTH); framesetSize( ); framesetVisible(true);*/ } /** *<br>方法说明初始化 *<br>输入参数isPath 是否可以输入带目录的文件名 *<br>返回类型 */ public JFileNameTextField(boolean isPath) { super(); FileNameDocument fd = (FileNameDocument)thisgetDocument(); fdsetIsPath(isPath); } protected Document createDefaultModel() { return new FileNameDocument(this); } } /** * 判断输入字符是否符合文件名规范的过滤实现 * @author dl */ class FileNameDocument extends PlainDocument { private boolean isPath = false; /**是否可以输入带目录的文件名*/ private JTextField parent = null; /**PlainDocument所在的文本框*/ private static Toolkit toolkit = ToolkitgetDefaultToolkit(); public FileNameDocument(JTextField field) { super(); thisparent = field; } public void setIsPath(boolean isPath) { thisisPath = isPath; } public void insertString(int offset String text AttributeSet attributes) throws BadLocationException { //得到当前文本框的内容 String strValue = parentgetText(); strValue = strValuesubstring( offset) + text + strValuesubstring(offset strValuelength()); //判断得到的文本是否符合文件名的语法规范 if (isPath) { if (!isNormallyPathString(strValue)) { toolkitbeep(); return; } } else { if (!isNormallyNameString(strValue)) { toolkitbeep(); return; } } superinsertString(offset text attributes); } /** *<br>方法说明判断文件路径是否合法 *<br>输入参数strName 文件路径 *<br>返回类型符合语法规则的文件路径 */ public static boolean isNormallyPathString(String strName) { int pos = strNameindexOf(:); if (strNameindexOf(\\\\) != ) return false; if (pos == ) { StringTokenizer st = new StringTokenizer(strName \\); while (sthasMoreTokens()) { String strTemp = stnextToken(); if (!isNormallyNameString(strTemp)) { return false; } } } else { String strPath = strNamesubstring( pos); if (strPathlength() == ) { javalangCharacter fq = strPathtoLowerCase()charAt(); javalangCharacter fq = strNametoLowerCase()charAt(pos); if (fq != :) return false; if (strNamelength() > pos + ) { javalangCharacter fq = strNametoLowerCase()charAt( pos + ); if (fq != \\) return false; } if (fq >= c && fq <= z) { if (!new File(fq + :)exists()) return false; } } else { return false; } StringTokenizer st = new StringTokenizer(strNamesubstring(pos + strNamelength()) \\); while (sthasMoreTokens()) { String strTemp = stnextToken(); if (!isNormallyNameString(strTemp)) { return false; } } } return true; } /** *<br>方法说明判断文件名是否合法 *<br>输入参数strName 文件名 *<br>返回类型符合语法规则的文件名 */ public static boolean isNormallyNameString(String strName) { int pos = strNameindexOf(:\\); if (pos == ) { } String strText = \t\r\n\\/:*?\<>|^___FCKpd___quot;; for (int i = ; i < strNamelength(); ++i) { String ch = StringvalueOf(strNamecharAt(i)); if (strTextindexOf(ch) != ) { return false; } } return true; } } package combrowser; import javaawtToolkit; import javaioFile; import javautilStringTokenizer; import javaxswingJTextField; import javaxswingtextAttributeSet; import javaxswingtextBadLocationException; import javaxswingtextDocument; import javaxswingtextPlainDocument; /** * 只能输入文件名的文本框 * @author dl */ public class JFileNameTextField extends javaxswingJTextField{ public static void main(String[] args) { // TODO Autogenerated method stub //测试代码 /*JFrame frame = new JFrame(文本框的内容限制测试); framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); frameadd(new JFileNameTextField(true) BorderLayoutCENTER); frameadd(new JNumTextField( ) BorderLayoutNORTH); framesetSize( ); framesetVisible(true);*/ } /** *<br>方法说明初始化 *<br>输入参数isPath 是否可以输入带目录的文件名 *<br>返回类型 */ public JFileNameTextField(boolean isPath) { super(); FileNameDocument fd = (FileNameDocument)thisgetDocument(); fdsetIsPath(isPath); } protected Document createDefaultModel() { return new FileNameDocument(this); } } /** * 判断输入字符是否符合文件名规范的过滤实现 * @author dl */ class FileNameDocument extends PlainDocument { private boolean isPath = false; /**是否可以输入带目录的文件名*/ private JTextField parent = null; /**PlainDocument所在的文本框*/ private static Toolkit toolkit = ToolkitgetDefaultToolkit(); public FileNameDocument(JTextField field) { super(); thisparent = field; } public void setIsPath(boolean isPath) { thisisPath = isPath; } public void insertString(int offset String text AttributeSet attributes) throws BadLocationException { //得到当前文本框的内容 String strValue = parentgetText(); strValue = strValuesubstring( offset) + text + strValuesubstring(offset strValuelength()); //判断得到的文本是否符合文件名的语法规范 if (isPath) { if (!isNormallyPathString(strValue)) { toolkitbeep(); return; } } else { if (!isNormallyNameString(strValue)) { toolkitbeep(); return; } } superinsertString(offset text attributes); } /** *<br>方法说明判断文件路径是否合法 *<br>输入参数strName 文件路径 *<br>返回类型符合语法规则的文件路径 */ public static boolean isNormallyPathString(String strName) { int pos = strNameindexOf(:); if (strNameindexOf(\\\\) != ) return false; if (pos == ) { StringTokenizer st = new StringTokenizer(strName \\); while (sthasMoreTokens()) { String strTemp = stnextToken(); if (!isNormallyNameString(strTemp)) { return false; } } } else { String strPath = strNamesubstring( pos); if (strPathlength() == ) { javalangCharacter fq = strPathtoLowerCase()charAt(); javalangCharacter fq = strNametoLowerCase()charAt(pos); if (fq != :) return false; if (strNamelength() > pos + ) { javalangCharacter fq = strNametoLowerCase()charAt( pos + ); if (fq != \\) return false; } if (fq >= c && fq <= z) { if (!new File(fq + :)exists()) return false; } } else { return false; } StringTokenizer st = new StringTokenizer(strNamesubstring(pos + strNamelength()) \\); while (sthasMoreTokens()) { String strTemp = stnextToken(); if (!isNormallyNameString(strTemp)) { return false; } } } return true; } /** *<br>方法说明判断文件名是否合法 *<br>输入参数strName 文件名 *<br>返回类型符合语法规则的文件名 */ public static boolean isNormallyNameString(String strName) { int pos = strNameindexOf(:\\); if (pos == ) { } String strText = \t\r\n\\/:*?\<>|^___FCKpd___quot;; for (int i = ; i < strNamelength(); ++i) { String ch = StringvalueOf(strNamecharAt(i)); if (strTextindexOf(ch) != ) { return false; } } return true; } } view plaincopy to clipboardprint? package combrowser; import javaawtToolkit; import javaxswingJTextField; import javaxswingtextAttributeSet; import javaxswingtextBadLocationException; import javaxswingtextDocument; import javaxswingtextPlainDocument; /** * 只能输入数字的文本框 * @author dl */ public class JNumTextField extends javaxswingJTextField { /** *<br>方法说明初始化 *<br>输入参数min 允许输入的数值的最小值; max 允许输入的数值的最大值 *<br>返回类型 */ public JNumTextField(int min int max) { super(); NumericDocument nd = (NumericDocument) thisgetDocument(); ndsetMax(max); ndsetMin(min); ndsetTextField(this); } protected Document createDefaultModel() { return new NumericDocument( ); } } /** * 判断输入内容是否属于允许范围内的数值的过滤实现 * @author dl */ class NumericDocument extends PlainDocument { protected int maxDigits = ; /**允许输入的数值的最大值*/ protected int minDigits = ; /**允许输入的数值的最小值*/ protected JTextField parent = null; /**PlainDocument所在的文本框*/ protected static Toolkit toolkit = ToolkitgetDefaultToolkit(); public NumericDocument(int min int max) { super(); maxDigits = max; minDigits = min; } public void setMax(int max) { maxDigits = max; } public void setMin(int min) { minDigits = min; } public void setTextField(JTextField parent) { thisparent = parent; } public void insertString(int offset String text AttributeSet attributes) throws BadLocationException { //得到当前文本框的内容 String strValue = parentgetText(); strValue = strValuesubstring( offset) + text + strValuesubstring(offset strValuelength()); //判断输入内容是否属于允许范围内的数值 int value = ; try { value = IntegerparseInt(strValue); } catch (NumberFormatException ex) { toolkitbeep(); return; } if (value > maxDigits || value < minDigits) { toolkitbeep(); return; } superinsertString(offset text attributes); } } |