怎样控制在JTextField中只能输入数字???

ghostqaz 2004-08-11 08:41:41
怎样控制在JTextField中只能输入数字,而不能输入汉字或字符呢




...全文
114 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
erictang2003 2004-08-11
  • 打赏
  • 举报
回复
try / catch 是非常消耗系统资源的,推荐方法是用正则表达式:

boolean b = Pattern.matches( "\d+", _JTextField.getText() );
if( !b ){
System.out.println( "not a number!" );
return;
}
wulemale 2004-08-11
  • 打赏
  • 举报
回复
上面有一点点错误:
int i;
try{i=Integer.parseInt(tx.getText()){
是数字就到这儿来了
}catch(NumberFormatException e){
不是到这儿
}
}
wulemale 2004-08-11
  • 打赏
  • 举报
回复
int i;
try{i=Integer.parseInt(tx.getText());
是数字就到这儿来了
}catch(NumberFormatException e){
不是到这儿
}
}

62,614

社区成员

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

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