谁能告诉我如何控制textarea的输入长度啊?我输入不了多少文字就不能加了。。

akingzhen 2003-08-19 02:26:11
RT,谢谢大家啊~~~帮帮我哈`~~
...全文
56 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
oopliu 2003-08-19
  • 打赏
  • 举报
回复
study!
whyxx 2003-08-19
  • 打赏
  • 举报
回复
flybird0923(追梦人) ( ) 的方法比较简单,效果也不错
bonnyone 2003-08-19
  • 打赏
  • 举报
回复
我决的给这个textarea加个KeyTyped接口,在接口函数中控制长度!
Sundery 2003-08-19
  • 打赏
  • 举报
回复
简单的话,我觉得用.size()或者.length()来控制也蛮好:)
gdsean 2003-08-19
  • 打赏
  • 举报
回复
one way u can do like this,

The following two classes are to create a textfield with
restricting the number of characters to be entered into it.
=============================================================
public class FixedLengthPlainDocument extends PlainDocument {

private int maxLength;

public FixedLengthPlainDocument(int maxLength) {
this.maxLength = maxLength;
}
public void insertString(int offset, String str,
AttributeSet a)
throws BadLocationException {
if(getLength() + str.length() > maxLength) {
Toolkit.getDefaultToolkit().beep();
}
else {
super.insertString(offset, str, a);
}
}

}

public class FixedLengthTextArea extends JTextArea{

public FixedLengthTextArea (int length)
{
this(null, length);
}

public FixedLengthTextArea (String text, int length) {
super(new FixedLengthPlainDocument(length), text,
length);
}
}
=========================================================

so now u can create ur text field as
JTextArea tf = new FixedLengthTextArea (10);

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧