如何限制JTextField中不能输入汉字?

解决方案 »

  1.   

    一个比较笨,但很管用的方法,截获键盘事件/内容变更事件,
    调用检测内容是否为汉字的代码
    请参考 :如何运用java来校验一段字符串是否为汉字
      

  2.   

      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; 
      }进制间转换没大看懂  :(
      

  3.   

    可以利用common-lang-2.3.jar包中的StringUtils.isAsciiPrintable()来判断
      

  4.   

     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