知识大全 只能输入文件名和只能输入数字的文本框
Posted 知
篇首语:学向勤中得,萤窗万卷书。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 只能输入文件名和只能输入数字的文本框相关的知识,希望对你有一定的参考价值。
package browser;
import java awt Toolkit;
import java io File;
import java util StringTokenizer;
import javax swing JTextField;
import javax swing text AttributeSet;
import javax swing text BadLocationException;
import javax swing text Document;
import javax swing text PlainDocument;
/**
* 只能输入文件名的文本框
* @author dl
*/
public class JFileNameTextField extends javax swing JTextField
public static void main(String[] args)
// TODO Auto generated method stub
//测试代码
/*JFrame frame = new JFrame( 文本框的内容限制测试 );
frame setDefaultCloseOperation(JFrame EXIT_ON_CLOSE);
frame add(new JFileNameTextField(true) BorderLayout CENTER);
frame add(new JNumTextField( ) BorderLayout NORTH);
frame setSize( );
frame setVisible(true);*/
/**
*<br>方法说明 初始化
*<br>输入参数 isPath 是否可以输入带目录的文件名
*<br>返回类型
*/
public JFileNameTextField(boolean isPath)
super();
FileNameDocument fd = (FileNameDocument)this getDocument();
fd setIsPath(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 = Toolkit getDefaultToolkit();
public FileNameDocument(JTextField field)
super();
this parent = field;
public void setIsPath(boolean isPath)
this isPath = isPath;
public void insertString(int offset String text AttributeSet attributes)
throws BadLocationException
//得到当前文本框的内容
String strValue = parent getText();
strValue = strValue substring( offset) + text
+ strValue substring(offset strValue length());
//判断得到的文本是否符合文件名的语法规范
if (isPath)
if (!isNormallyPathString(strValue))
toolkit beep();
return;
else
if (!isNormallyNameString(strValue))
toolkit beep();
return;
super insertString(offset text attributes);
/**
*<br>方法说明 判断文件路径是否合法
*<br>输入参数 strName 文件路径
*<br>返回类型 符合语法规则的文件路径
*/
public static boolean isNormallyPathString(String strName)
int pos = strName indexOf( : );
if (strName indexOf( \\\\\\\\ ) != )
return false;
if (pos == )
StringTokenizer st = new StringTokenizer(strName \\\\ );
while (st hasMoreTokens())
String strTemp = st nextToken();
if (!isNormallyNameString(strTemp))
return false;
else
String strPath = strName substring( pos);
if (strPath length() == )
java lang Character fq = strPath toLowerCase() charAt( );
java lang Character fq = strName toLowerCase() charAt(pos);
if (fq != : )
return false;
if (strName length() > pos + )
java lang Character fq = strName toLowerCase() 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(strName substring(pos +
strName length()) \\\\ );
while (st hasMoreTokens())
String strTemp = st nextToken();
if (!isNormallyNameString(strTemp))
return false;
return true;
/**
*<br>方法说明 判断文件名是否合法
*<br>输入参数 strName 文件名
*<br>返回类型 符合语法规则的文件名
*/
public static boolean isNormallyNameString(String strName)
int pos = strName indexOf( :\\\\ );
if (pos == )
String strText = \\t\\r\\n\\\\/:*?\\ <>|^___FCKpd___ quot;;
for (int i = ; i < strName length(); ++i)
String ch = String valueOf(strName charAt(i));
if (strText indexOf(ch) != )
return false;
return true;
package browser;
import java awt Toolkit;
import java io File;
import java util StringTokenizer;
import javax swing JTextField;
import javax swing text AttributeSet;
import javax swing text BadLocationException;
import javax swing text Document;
import javax swing text PlainDocument;
/**
* 只能输入文件名的文本框
* @author dl
*/
public class JFileNameTextField extends javax swing JTextField
public static void main(String[] args)
// TODO Auto generated method stub
//测试代码
/*JFrame frame = new JFrame( 文本框的内容限制测试 );
frame setDefaultCloseOperation(JFrame EXIT_ON_CLOSE);
frame add(new JFileNameTextField(true) BorderLayout CENTER);
frame add(new JNumTextField( ) BorderLayout NORTH);
frame setSize( );
frame setVisible(true);*/
/**
*<br>方法说明 初始化
*<br>输入参数 isPath 是否可以输入带目录的文件名
*<br>返回类型
*/
public JFileNameTextField(boolean isPath)
super();
FileNameDocument fd = (FileNameDocument)this getDocument();
fd setIsPath(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 = Toolkit getDefaultToolkit();
public FileNameDocument(JTextField field)
super();
this parent = field;
public void setIsPath(boolean isPath)
this isPath = isPath;
public void insertString(int offset String text AttributeSet attributes)
throws BadLocationException
//得到当前文本框的内容
String strValue = parent getText();
strValue = strValue substring( offset) + text
+ strValue substring(offset strValue length());
//判断得到的文本是否符合文件名的语法规范
if (isPath)
if (!isNormallyPathString(strValue))
toolkit beep();
return;
else
if (!isNormallyNameString(strValue))
toolkit beep();
return;
super insertString(offset text attributes);
/**
*<br>方法说明 判断文件路径是否合法
*<br>输入参数 strName 文件路径
*<br>返回类型 符合语法规则的文件路径
*/
public static boolean isNormallyPathString(String strName)
int pos = strName indexOf( : );
if (strName indexOf( \\\\\\\\ ) != )
return false;
if (pos == )
StringTokenizer st = new StringTokenizer(strName \\\\ );
while (st hasMoreTokens())
String strTemp = st nextToken();
if (!isNormallyNameString(strTemp))
return false;
else
String strPath = strName substring( pos);
if (strPath length() == )
java lang Character fq = strPath toLowerCase() charAt( );
java lang Character fq = strName toLowerCase() charAt(pos);
if (fq != : )
return false;
if (strName length() > pos + )
java lang Character fq = strName toLowerCase() 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(strName substring(pos +
strName length()) \\\\ );
while (st hasMoreTokens())
String strTemp = st nextToken();
if (!isNormallyNameString(strTemp))
return false;
return true;
/**
*<br>方法说明 判断文件名是否合法
*<br>输入参数 strName 文件名
*<br>返回类型 符合语法规则的文件名
*/
public static boolean isNormallyNameString(String strName)
int pos = strName indexOf( :\\\\ );
if (pos == )
String strText = \\t\\r\\n\\\\/:*?\\ <>|^___FCKpd___ quot;;
for (int i = ; i < strName length(); ++i)
String ch = String valueOf(strName charAt(i));
if (strText indexOf(ch) != )
return false;
return true;
view plaincopy to clipboardprint? package browser;
import java awt Toolkit;
import javax swing JTextField;
import javax swing text AttributeSet;
import javax swing text BadLocationException;
import javax swing text Document;
import javax swing text PlainDocument;
/**
* 只能输入数字的文本框
* @author dl
*/
public class JNumTextField extends javax swing JTextField
/**
*<br>方法说明 初始化
*<br>输入参数 min 允许输入的数值的最小值; max 允许输入的数值的最大值
*<br>返回类型
*/
public JNumTextField(int min int max)
super();
NumericDocument nd = (NumericDocument) this getDocument();
nd setMax(max);
nd setMin(min);
nd setTextField(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 = Toolkit getDefaultToolkit();
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)
this parent = parent;
public void insertString(int offset String text AttributeSet attributes)
throws BadLocationException
//得到当前文本框的内容
String strValue = parent getText();
strValue = strValue substring( offset) + text
+ strValue substring(offset strValue length());
//判断输入内容是否属于允许范围内的数值
int value = ;
try
value = Integer parseInt(strValue);
catch (NumberFormatException ex)
toolkit beep();
return;
if (value > maxDigits || value < minDigits)
toolkit beep();
return;
super insertString(offset text attributes);
cha138/Article/program/Java/hx/201311/25541
相关参考