JTextField的问题

wangju466511977 2008-03-08 02:27:10
如何限制JTextField中不能输入汉字?
...全文
93 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hmsuccess 2008-03-08
  • 打赏
  • 举报
回复
public static boolean isGB2312(String str) {
char[] chars = str.toCharArray();
boolean isGB2312 = false;
for (int i = 0; i < chars.length; i++) {
byte[] bytes = (chars + "").getBytes();
if (bytes.length == 2) {
int[] ints = new int[2];
ints[0] = bytes[0] & 0xff;
ints[1] = bytes[1] & 0xff;
if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE) {
isGB2312 = true;
break;
}
}
}
return isGB2312;
}

我试一下,好像什么都会输出false
huoyin 2008-03-08
  • 打赏
  • 举报
回复
可以利用common-lang-2.3.jar包中的StringUtils.isAsciiPrintable()来判断
haisenmai 2008-03-08
  • 打赏
  • 举报
回复
public static boolean isGB2312(String str) {
char[] chars = str.toCharArray();
boolean isGB2312 = false;
for (int i = 0; i < chars.length; i++) {
byte[] bytes = ("" + chars).getBytes();
if (bytes.length == 2) {
int[] ints = new int[2];
ints[0] = bytes[0] & 0xff;
ints[1] = bytes[1] & 0xff;
if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE) {
isGB2312 = true;
break;
}
}
}
return isGB2312;
}

进制间转换没大看懂 :(
老紫竹 2008-03-08
  • 打赏
  • 举报
回复
一个比较笨,但很管用的方法,截获键盘事件/内容变更事件,
调用检测内容是否为汉字的代码
请参考 :如何运用java来校验一段字符串是否为汉字

62,623

社区成员

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

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