package com
import java
import java
import java
import javax
import javax
import javax
import javax
import javax
/**
* 只能輸入文件名的文本框
* @author dl
*/
public class JFileNameTextField extends javax
public static void main(String[] args) {
// TODO Auto
//測試代碼
/*JFrame frame = new JFrame(
frame
frame
frame
frame
frame
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public JFileNameTextField(boolean isPath) {
super();
FileNameDocument fd = (FileNameDocument)this
fd
}
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
public FileNameDocument(JTextField field) {
super();
this
}
public void setIsPath(boolean isPath) {
this
}
public void insertString(int offset
throws BadLocationException {
//得到當前文本框的內容
String strValue = parent
strValue = strValue
+ strValue
//判斷得到的文本是否符合文件名的語法規范
if (isPath) {
if (!isNormallyPathString(strValue)) {
toolkit
return;
}
} else {
if (!isNormallyNameString(strValue)) {
toolkit
return;
}
}
super
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public static boolean isNormallyPathString(String strName) {
int pos = strName
if (strName
return false;
if (pos ==
StringTokenizer st = new StringTokenizer(strName
while (st
String strTemp = st
if (!isNormallyNameString(strTemp)) {
return false;
}
}
} else {
String strPath = strName
if (strPath
java
java
if (fq
return false;
if (strName
java
pos +
if (fq
return false;
}
if (fq >=
if (!new File(fq +
return false;
}
} else {
return false;
}
StringTokenizer st = new StringTokenizer(strName
strName
while (st
String strTemp = st
if (!isNormallyNameString(strTemp)) {
return false;
}
}
}
return true;
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public static boolean isNormallyNameString(String strName) {
int pos = strName
if (pos ==
}
String strText =
for (int i =
String ch = String
if (strText
return false;
}
}
return true;
}
}
package com
import java
import java
import java
import javax
import javax
import javax
import javax
import javax
/**
* 只能輸入文件名的文本框
* @author dl
*/
public class JFileNameTextField extends javax
public static void main(String[] args) {
// TODO Auto
//測試代碼
/*JFrame frame = new JFrame(
frame
frame
frame
frame
frame
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public JFileNameTextField(boolean isPath) {
super();
FileNameDocument fd = (FileNameDocument)this
fd
}
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
public FileNameDocument(JTextField field) {
super();
this
}
public void setIsPath(boolean isPath) {
this
}
public void insertString(int offset
throws BadLocationException {
//得到當前文本框的內容
String strValue = parent
strValue = strValue
+ strValue
//判斷得到的文本是否符合文件名的語法規范
if (isPath) {
if (!isNormallyPathString(strValue)) {
toolkit
return;
}
} else {
if (!isNormallyNameString(strValue)) {
toolkit
return;
}
}
super
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public static boolean isNormallyPathString(String strName) {
int pos = strName
if (strName
return false;
if (pos ==
StringTokenizer st = new StringTokenizer(strName
while (st
String strTemp = st
if (!isNormallyNameString(strTemp)) {
return false;
}
}
} else {
String strPath = strName
if (strPath
java
java
if (fq
return false;
if (strName
java
pos +
if (fq
return false;
}
if (fq >=
if (!new File(fq +
return false;
}
} else {
return false;
}
StringTokenizer st = new StringTokenizer(strName
strName
while (st
String strTemp = st
if (!isNormallyNameString(strTemp)) {
return false;
}
}
}
return true;
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public static boolean isNormallyNameString(String strName) {
int pos = strName
if (pos ==
}
String strText =
for (int i =
String ch = String
if (strText
return false;
}
}
return true;
}
}
view plaincopy to clipboardprint?
package com
import java
import javax
import javax
import javax
import javax
import javax
/**
* 只能輸入數字的文本框
* @author dl
*/
public class JNumTextField extends javax
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public JNumTextField(int min
super();
NumericDocument nd = (NumericDocument) this
nd
nd
nd
}
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
public NumericDocument(int min
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
}
public void insertString(int offset
throws BadLocationException {
//得到當前文本框的內容
String strValue = parent
strValue = strValue
+ strValue
//判斷輸入內容是否屬於允許范圍內的數值
int value =
try {
value = Integer
} catch (NumberFormatException ex) {
toolkit
return;
}
if (value > maxDigits || value < minDigits) {
toolkit
return;
}
super
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25541.html