TextField输入数字及星号的问题。

programfish 2007-09-03 08:34:32
今天看了一个程序里有个密码输入TextField,输入的时候显示的是星号,但是又控制了只能输入数字,不知是怎么实现的,我知道用.password是星号,但是不能限制只允许输入数字,请教各位大虾如何实现这个功能啊。

...全文
255 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
programfish 2007-09-03
  • 打赏
  • 举报
回复
呃,能注释一下么。
believefym 2007-09-03
  • 打赏
  • 举报
回复
参考下面的


a = new JTextField(5);

a.setDocument(new OnlyDigit(a));



/**
* this class is used to restrict the input, it makes sure that
* the text of the text field is a digit string.that is, 'a','Z',
* '_','$'and so on are all forbidden.
* @author feng
*
*/
private class OnlyDigit extends PlainDocument{
private JTextField f;

public OnlyDigit(JTextField f){
this.f = f;
}
public void insertString(int offset,
String str,
AttributeSet attSet)
throws BadLocationException{
StringBuffer tmp = new StringBuffer(f.getText());
tmp.insert(offset,str);
Pattern p = Pattern.compile("^-?\\d*(\\.)?\\d*$");
Matcher m = p.matcher(tmp.toString());
if(m.find()){
super.insertString(offset,str,attSet);
}
}
}

13,100

社区成员

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

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