maxLengh自己写,setFocus直接调。

解决方案 »

  1.   

    TO stonegump(龙飞虎) 
    setFocus直接调,不知道怎么调用啊.
      

  2.   

    package TextBox;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.PlainDocument;
    import TextBox.*;public class TextBox extends JTextField
    {
        private int iMaxLen;    public TextBox()
        {
            super();
            LoadEvent();
        }    public void setMaxLength(int iMaxLength)
        {
            iMaxLen = iMaxLength;
        }
        public void LoadEvent()
        {
            this.addFocusListener(new FocusListener(){
                public void focusLost(FocusEvent e)
                {
                    LostFocus();
                }
                public void focusGained(FocusEvent e)
                {
                    GetFocus();
                }
            });        this.addKeyListener(new MaxLengthKeyListener());
        }
        public void LostFocus()
        {
            if(!IsNumber(this.getText()))
            {
                this.requestFocus();
            }
        }    public void GetFocus()
        {
            this.selectAll();
        }    public static  boolean IsNumber(String  sTmp)
        {
            String sNumber[]={"0","1","2","3","4","5","6","7","8","9"};
            String sComp="";        for(int j=0;j<sTmp.length();j++ )
            {
                sComp=sTmp.substring(j,j+1);
                for(int i=0;i <10 ;i++ )
                {
                    if( sComp.equals(sNumber[i]) )
                    {
                        break;
                    }
                    if ( i == 9)
                    {
                        return false;
                    }
                }
            }
            return true;
        }    protected Document createDefaultModel()
        {
            return new MaxLengthDocument();
        }    class MaxLengthDocument extends PlainDocument
        {
            private int added = 0;        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException
            {
                if (arg0 < iMaxLen)
                {
                    super.insertString(arg0, arg1, arg2);
                }
            }
        }    class MaxLengthKeyListener implements KeyListener
        {
            private String oldString;        public void keyPressed(KeyEvent arg0)
            {
                checkMaxLength();
            }        public void keyReleased(KeyEvent arg0)
            {
                checkMaxLength();
            }        public void keyTyped(KeyEvent arg0)
            {
                checkMaxLength();
            }        private void checkMaxLength()
            {
                String checkString = getText();
                byte[] byteLength = checkString.getBytes();
                if (byteLength.length > iMaxLen)
                {
                    setText(oldString);
                } else {
                    oldString = checkString;
                }
            }
        }
    }