class ParseTextDocument extends PlainDocument
{ public void insertString(int offs, String str,
AttributeSet a) throws BadLocationException
{
if (str == null) return;
int size = getLength() + str.length();
if (size <= maxSize && m.matches() || str.length() > 1)
{
super.insertString(offs, str, a);
}
}
} private ParseTextDocument xxField;
这样xxField就可以过滤输入了!
{ public void insertString(int offs, String str,
AttributeSet a) throws BadLocationException
{
if (str == null) return;
int size = getLength() + str.length();
if (size <= maxSize && m.matches() || str.length() > 1)
{
super.insertString(offs, str, a);
}
}
} private ParseTextDocument xxField;
这样xxField就可以过滤输入了!
int size; //当前的宽度 Pattern p = Pattern.compile("[0-9()-]");
Matcher m = p.matcher("");你说的是不是在前面这个前提下 : 用你说的这个类阿!?你的方法,和我说的方法之间有什么区别阿!!thank you!!
比如我在另外一个Java程序里有个:JTextField txtfldName = JTextField();怎样把这个txtfldName用这个类格式化阿!?thank you!!
所以应该extends PlainDocument!
你不难看出我所给的类中size <= maxSize && m.matches() 是在你给的前提下的,你可以在ParseTextDocument 类中转入这两个参数;
至于 在格式化一个一个JTextField 时,该怎么格式化阿!?
你可以创建一个MyJTextField extends JTextField
然后MyJTextField txtfldName = MyJTextField ()不就可以吗?
{
public MyJTextField ()
{
super();
} protected Document createDefaultModel() //重载这个方法即可!
{
return new ParseTextDocument();
}
}