关于JTextFeild 我试了,在JAVA中好像不能区分中音文,关注这个问题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JTextField jtf=new JTextField(20);jtf.setDocument(yourDocument);class yourDocument extends PlainDocument {………………} sorryjtf.setDocument(new yourDocument()); to:beyond_xiron()能不能把DOCUMENT写出来啊我试着写了,可是没有用啊 给textfield注册一个Document Listener,在侦听器里边写当输入超过上限时的方法。 我自己写过一个类,LimitLengthDocument,可以设置你所希望限制的长度。如果maxLength = 0,就是不限制长度。import javax.swing.text.*;public class LimitLengthDocument extends PlainDocument { private int maxLength = 0; public LimitLengthDocument(){ } public LimitLengthDocument(int maxLength) { super(); if (maxLength < 1) { this.maxLength = 0; } else{ this.maxLength = maxLength; } } //重写父类的insertString函数 public void insertString(int offset,String str,AttributeSet a)throws BadLocationException{ String strBeforeInsert = getText(0, getLength()); String strAfterInsert = strBeforeInsert.substring(0, offset) + str + strBeforeInsert.substring(offset); //如果插入字符创str后,文档超长,则插入失败 if (maxLength == 0 || strAfterInsert.length() <= maxLength){ super.insertString(offset, str, a); } } public int getMaxLength(){ return this.maxLength; } public void setMaxLength(int maxLength){ if (maxLength <1) { this.maxLength = 0; } else{ this.maxLength = maxLength; } if (this.getLength()> maxLength && maxLength > 0) { try { this.remove(maxLength, this.getLength() - maxLength); } catch (BadLocationException ex) { java.awt.Toolkit.getDefaultToolkit().beep(); } } }}然后这样JTextField txtTest=new JTextField();txtTest.setDocument(new LimitLengthDocument(4)); 好象java不区分中英文的,我在vb里也碰到过这个问题,后来直接用读取ascii方法来的,不过编程量挺大的,你试试吧 在线等答案,急! jbutton ctrl+enter 快捷键 请问如何获得屏幕信息? 一个非常有趣的问题,路过不可错过,而且有分哦!! jni问题,高分请教!!! 数组传值调用这样写有错没有啊?急啊~~~~~~ 求编译时间的问题 有关HashMap的一个method:entrySet() 请教关于equals的问题 java执行顺序的控制问题! 大家帮忙解决一下!有4个错误 这个问题怎么搞定?是不是CLASSPATH没有设定好?
jtf.setDocument(yourDocument);
class yourDocument extends PlainDocument {
………………
}
jtf.setDocument(new yourDocument());
能不能把DOCUMENT写出来啊
我试着写了,可是没有用啊
如果maxLength = 0,就是不限制长度。import javax.swing.text.*;
public class LimitLengthDocument extends PlainDocument {
private int maxLength = 0; public LimitLengthDocument(){
} public LimitLengthDocument(int maxLength) {
super();
if (maxLength < 1) {
this.maxLength = 0;
}
else{
this.maxLength = maxLength;
}
} //重写父类的insertString函数
public void insertString(int offset,String str,AttributeSet a)throws BadLocationException{ String strBeforeInsert = getText(0, getLength());
String strAfterInsert = strBeforeInsert.substring(0, offset) +
str +
strBeforeInsert.substring(offset);
//如果插入字符创str后,文档超长,则插入失败
if (maxLength == 0 || strAfterInsert.length() <= maxLength){
super.insertString(offset, str, a);
}
} public int getMaxLength(){
return this.maxLength;
} public void setMaxLength(int maxLength){
if (maxLength <1) {
this.maxLength = 0;
}
else{
this.maxLength = maxLength;
} if (this.getLength()> maxLength && maxLength > 0) {
try {
this.remove(maxLength, this.getLength() - maxLength);
}
catch (BadLocationException ex) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}
}
然后这样
JTextField txtTest=new JTextField();
txtTest.setDocument(new LimitLengthDocument(4));