重新定制一个Document ,
class youDocument extends javax.swing.text.PlainDocument {
int maxLen = 20;
public youDocument(int maxLen){
this.maxLen = maxLen;
}
public void insertString(int offset, String s, javax.swing.text.AttributeSet attributeSet) throws javax.swing.text.BadLocationException {
// 判断长度
String strLastText = super.getText(0,super.getLength());
if(strLastTest.length() >= maxLen)return ;
if(s.length()+ strLastText.length() >maxLen)return;
}
}
JText.setDocument(new youDocument(5));以上代码仅供参考.
class youDocument extends javax.swing.text.PlainDocument {
int maxLen = 20;
public youDocument(int maxLen){
this.maxLen = maxLen;
}
public void insertString(int offset, String s, javax.swing.text.AttributeSet attributeSet) throws javax.swing.text.BadLocationException {
// 判断长度
String strLastText = super.getText(0,super.getLength());
if(strLastTest.length() >= maxLen)return ;
if(s.length()+ strLastText.length() >maxLen)return;
}
}
JText.setDocument(new youDocument(5));以上代码仅供参考.
public JFormattedTextField(Format format),看一下jb中的帮助,没装JB的话就看SUN
的文档就行了。