在 JTextField 中键入阿拉伯数字
我试图在 JTextField
中输入阿拉伯数字,并使用 DocumentListener
如下:
txtName.getDocument().addDocumentListener(this);
...
public void insertUpdate(DocumentEvent e){setLabel();}
public void removeUpdate(DocumentEvent e){setLabel();}
public void changedUpdate(DocumentEvent e){}
public void setLabel()
{
String s = txtName.getText();
s = s.replace('0','\u0660');
s = s.replace('1','\u0661');
s = s.replace('2','\u0662');
s = s.replace('3','\u0663');
s = s.replace('4','\u0664');
s = s.replace('5','\u0665');
s = s.replace('6','\u0666');
s = s.replace('7','\u0667');
s = s.replace('8','\u0668');
s = s.replace('9','\u0669');
s = s.replace('.',',');
txtName.setText(s);
}
但我在 txtName.setText(s);
处收到错误>
错误是:
Exception occurred during event dispatching:
java.lang.IllegalStateException: Attempt to mutate in notification
I am trying to type Arabic numbers in a JTextField
and I used DocumentListener
as follows:
txtName.getDocument().addDocumentListener(this);
...
public void insertUpdate(DocumentEvent e){setLabel();}
public void removeUpdate(DocumentEvent e){setLabel();}
public void changedUpdate(DocumentEvent e){}
public void setLabel()
{
String s = txtName.getText();
s = s.replace('0','\u0660');
s = s.replace('1','\u0661');
s = s.replace('2','\u0662');
s = s.replace('3','\u0663');
s = s.replace('4','\u0664');
s = s.replace('5','\u0665');
s = s.replace('6','\u0666');
s = s.replace('7','\u0667');
s = s.replace('8','\u0668');
s = s.replace('9','\u0669');
s = s.replace('.',',');
txtName.setText(s);
}
but I got an error at txtName.setText(s);
and the error was:
Exception occurred during event dispatching:
java.lang.IllegalStateException: Attempt to mutate in notification
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您阅读了 DocumentListener API,你会明白为什么会出现这个错误:
考虑使用 DocumentFilter 代替。
例如,
If you read the DocumentListener API, you'll see why this error occurred:
Consider using a DocumentFilter instead.
e.g.,