我试了,在JAVA中好像不能区分中音文,关注这个问题

解决方案 »

  1.   

    JTextField jtf=new JTextField(20);
    jtf.setDocument(yourDocument);
    class yourDocument extends PlainDocument {
    ………………
    }
      

  2.   

    sorry
    jtf.setDocument(new yourDocument());
      

  3.   

    to:beyond_xiron()
    能不能把DOCUMENT写出来啊
    我试着写了,可是没有用啊
      

  4.   

    给textfield注册一个Document Listener,在侦听器里边写当输入超过上限时的方法。
      

  5.   

    我自己写过一个类,LimitLengthDocument,可以设置你所希望限制的长度。
    如果maxLength = 0,就是不限制长度。import javax.swing.text.*;
    public class LimitLengthDocument extends PlainDocument {
        private int maxLength = 0;    public LimitLengthDocument(){
        }    public LimitLengthDocument(int maxLength) {
            super();
            if (maxLength < 1) {
                this.maxLength = 0;
            }
            else{
                this.maxLength = maxLength;
            }
        }    //重写父类的insertString函数
        public void insertString(int offset,String str,AttributeSet a)throws BadLocationException{        String strBeforeInsert = getText(0, getLength());
            String strAfterInsert = strBeforeInsert.substring(0, offset) +
                                    str +
                                    strBeforeInsert.substring(offset);
            //如果插入字符创str后,文档超长,则插入失败
            if (maxLength == 0 || strAfterInsert.length() <= maxLength){
                super.insertString(offset, str, a);
            }
        }    public int getMaxLength(){
            return this.maxLength;
        }    public void setMaxLength(int maxLength){
            if (maxLength <1) {
                this.maxLength = 0;
            }
            else{
                this.maxLength = maxLength;
            }        if (this.getLength()> maxLength && maxLength > 0) {
                try {
                    this.remove(maxLength, this.getLength() - maxLength);
                }
                catch (BadLocationException ex) {
                    java.awt.Toolkit.getDefaultToolkit().beep();
                }
            }
        }
    }
    然后这样
    JTextField txtTest=new JTextField();
    txtTest.setDocument(new LimitLengthDocument(4));
      

  6.   

    好象java不区分中英文的,我在vb里也碰到过这个问题,后来直接用读取ascii方法来的,不过编程量挺大的,你试试吧