java怎样实施监听jtextArea中输入的字符长度,如果长度大于指定的值,就自动将后面多余的内容剪裁掉。
以下是我写的代码,但为什么每次输入的字符大于指定的长度就会出错?classJtextSummaryListenerimplementsDocumentListener{i...
以下是我写的代码,但为什么每次输入的字符大于指定的长度就会出错?
class JtextSummaryListener implements DocumentListener{
int sum ;//总字数
int i;//当前字数
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
int sum = 10;
i = jTextAreaSummary.getText().length();//以输入的字符个数
if(i<=10){
jLableFont.setText(sum-i+"");
}
if(i>10){
String st = jTextAreaSummary.getText().substring(1, sum+1);
jTextAreaSummary.setText(st);
}
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
int sum = 10;
int i = jTextAreaSummary.getText().length();//以输入的字符个数
if(i<=10){
jLableFont.setText(sum-i+"");
}
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
}
错误提示:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(Unknown Source)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.setText(Unknown Source) 展开
class JtextSummaryListener implements DocumentListener{
int sum ;//总字数
int i;//当前字数
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
int sum = 10;
i = jTextAreaSummary.getText().length();//以输入的字符个数
if(i<=10){
jLableFont.setText(sum-i+"");
}
if(i>10){
String st = jTextAreaSummary.getText().substring(1, sum+1);
jTextAreaSummary.setText(st);
}
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
int sum = 10;
int i = jTextAreaSummary.getText().length();//以输入的字符个数
if(i<=10){
jLableFont.setText(sum-i+"");
}
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
}
错误提示:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(Unknown Source)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.setText(Unknown Source) 展开
2个回答
2013-11-26
展开全部
JTextArea myTextArea = new JTextArea(new MaxLengthDocument(30));
class MaxLengthDocument extends PlainDocument{
int maxChars;
public MaxLengthDocument(int max){
maxChars = max;
}
public void insertString(int offset, String s, AttributeSet a)throws BadLocationException{
if(getLength()+ s.length() > maxChars){
Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offset,s,a);
}
}
这样好用。
class MaxLengthDocument extends PlainDocument{
int maxChars;
public MaxLengthDocument(int max){
maxChars = max;
}
public void insertString(int offset, String s, AttributeSet a)throws BadLocationException{
if(getLength()+ s.length() > maxChars){
Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offset,s,a);
}
}
这样好用。
追问
为什么这一句“new MaxLengthDocument(30));”总是报错啊?提示:No enclosing instance of type JtextAreaTest is accessible. Must qualify theJtextAreaTest (e.g. x.new A() where x is an instance of JtextAreaTest).
追答
你的写法问题,写得不完整。
class MaxLengthDocument extends PlainDocument{
int maxChars;
public MaxLengthDocument(int max){
maxChars = max;
}
public void insertString(int offset, String s, AttributeSet a)throws BadLocationException{
if(getLength()+ s.length() > maxChars){
Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offset,s,a);
}
}
public class JTextAreaLimit extends JFrame{
public JTextAreaLimit(){
super("JTextAreaLimit");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea myTextArea = new JTextArea(new MaxLengthDocument(30));
this.add(myTextArea);
this.setVisible(true);
}
public static void main(String args[])throws Exception{
new JTextAreaLimit();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询