现在的问题是在我的这段代码里怎么判断我目前已经输入了15个字符,
我采用了这种方法,但是总有问题,就是当我录入超过15个字符后,我把刚才录入的内容清空(或删除几个),这时还是不能录入字符,因为我定义的变量是类变量(Static变量),我刚才说的情况怎么判断呢?我的代码如下:public class JTelDocument extends PlainDocument
{
public static int intCount = 0;
//重载insertString方法,限制它只能输入0-9数字、"("、")"、"-"符号
public void insertString(int index, String str, AttributeSet as) throws BadLocationException
{
if ((str == null) || (str.length() == 0))
{
intCount = 0;
return;
}
if (intCount > 15)
return; char[] input = str.toCharArray(); //转化成字符数组
int length = 0; //合法输入的数组长度
for (int i = 0; i < input.length; i++)
{
char ch = input[i];
if (ch >= '0' && ch <= '9')
{
input[length++] = ch;
intCount++;
}
if (ch == '(' || ch == ')' || ch == '-')
{
input[length++] = ch;
intCount++;
}
}
super.insertString(index, new String(input, 0, length), as);
}
}
我采用了这种方法,但是总有问题,就是当我录入超过15个字符后,我把刚才录入的内容清空(或删除几个),这时还是不能录入字符,因为我定义的变量是类变量(Static变量),我刚才说的情况怎么判断呢?我的代码如下:public class JTelDocument extends PlainDocument
{
public static int intCount = 0;
//重载insertString方法,限制它只能输入0-9数字、"("、")"、"-"符号
public void insertString(int index, String str, AttributeSet as) throws BadLocationException
{
if ((str == null) || (str.length() == 0))
{
intCount = 0;
return;
}
if (intCount > 15)
return; char[] input = str.toCharArray(); //转化成字符数组
int length = 0; //合法输入的数组长度
for (int i = 0; i < input.length; i++)
{
char ch = input[i];
if (ch >= '0' && ch <= '9')
{
input[length++] = ch;
intCount++;
}
if (ch == '(' || ch == ')' || ch == '-')
{
input[length++] = ch;
intCount++;
}
}
super.insertString(index, new String(input, 0, length), as);
}
}
import javax.swing.*;
import javax.swing.event.*;/**
* <p>Title: Control of the text field!
* <p>Description: In this text field,
* if the key that you push is not a number,
* it will can't be disposed in the field!
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 科大恒星
* @author 呉志強
* @version 1.0
*/public class OnlyInputNumberField extends JTextField
{
int givenLenght;
/** If the key have been pushed it will have do something.
* @param e The event of the key pushed.
*/
public OnlyInputNumberField() {
this.addKeyListener(new KeyAdapter() { /**
* This method it will judge
* whether or not the key you pushed is one of the keys of number
* @param e The key event that you input
*/
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();//Get name of the key that have been pushed.
String textValue = OnlyInputNumberField.this.getText();
int len = textValue.length();
if (c =='\b')
{
textValue=textValue.substring(0,len);
OnlyInputNumberField.this.setText(textValue);
}
else
{
if (c >= '0' && c <= '9')
return;
else
e.consume();
}
}
});
} public OnlyInputNumberField(int i)
{
givenLenght = i; this.addKeyListener(new KeyAdapter()
{
/**
* This method it will judge
* whether or not the lenght is over the given lenght
* @param e The key event that you push.
*/
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();//Get name of the key that have been pushed.
String textValue = OnlyInputNumberField.this.getText();
int len = textValue.length();
if (c =='\b')
{
textValue=textValue.substring(0,len);
OnlyInputNumberField.this.setText(textValue);
}
else
{
if (c >= '0' && c <= '9')
{
if (len < givenLenght) {
//System.out.println("the number");
return;
}
else
e.consume();
}
else
e.consume();
}
}
});
}}
import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class DocSizeFilter extends JFrame {
public static final int MAX_SIZE = 10;
private JTextField jtf; public DocSizeFilter() {
AbstractDocument aDoc; jtf = new JTextField();
aDoc = (AbstractDocument) jtf.getDocument();
aDoc.setDocumentFilter(new SizeFilter(MAX_SIZE));
getContentPane().add(jtf);
} public static void main(String[] args) {
DocSizeFilter frame = new DocSizeFilter();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
} private class SizeFilter extends DocumentFilter {
int maxSize;
int size; public SizeFilter(int maxSize) {
this.maxSize = maxSize;
} public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
size = fb.getDocument().getLength() + string.length();
if (size < maxSize) {
super.insertString(fb, offset, string, attr);
}
} public void replace(DocumentFilter.FilterBypass fb, int offset,
int length,String text, AttributeSet attrs)
throws BadLocationException {
size = fb.getDocument().getLength() + text.length();
if (size < maxSize) {
super.insertString(fb, offset, text, attrs);
}
}
}
}
int maxSize;
int size;
Pattern p = Pattern.compile("[0-9()-]");
Matcher m = p.matcher("");
public SizeFilter(int maxSize) {
this.maxSize = maxSize;
}
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
size = fb.getDocument().getLength() + string.length();
m.reset(string);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, string, attr);
}
}
public void replace(DocumentFilter.FilterBypass fb, int offset,
int length,String text, AttributeSet attrs)
throws BadLocationException {
size = fb.getDocument().getLength() + text.length();
m.reset(text);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, text, attrs);
}
}
} 用这个替换上面那个 SizeFilter 可以完全达到你的要求
jTextFiled.setDocument(new PlainDocument(){
public void insertString (int offset, String str, AttributeSet a) {
try {
if (this.getLength() + str.length() > 15){
return;
}
else{
super.insertString(offset, str, a);
}
}
catch (Exception exp) {
}
}
});
就可以了.我实验过,对于字母,数字还有符号都可以.
不过,之后出现了一个大BUG,我试验了很多方法,怀疑是SWING的问题,就是输入全角字符会导致之前输入的文字被删除.好象是自动的.不管是输入中文或者日文都一样.郁闷中