应该是super.setText("");吧,你的子类里并没有setText()方法

解决方案 »

  1.   

    用JFormattedTextField很容易实现你的要求的。也可以自己写的代码如下:
    -------------------------------转载-----------------------------------
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.PlainDocument;/**
     * This class is used to show a sub IP address in which a lot of defining
     * conditions have been inserted.
     *
     * @author RalphHu
     */
    public class IPTextField extends JTextField {
        /** The constructor of the text() */
        public IPTextField() {
            setDocument(new IPDocument());
        }    /**
         * An internal class which extends PlainDocument.
         *
         * @author RalphHu
         */
        private class IPDocument extends PlainDocument {
            /**
             * Inherit inserString method to check if the input string is legal
             * for IP address.
             *
             * @param offs the off from where the string will be added.
             * @param str the string inputed in.
             * @param a the attribute of the document.
             * @throws BadLocationException if the offs if improper, throw this
             * exception.
             */
            public void insertString(int offs, String str, AttributeSet a) throws
                    BadLocationException {
                if (str == null) {
                    return;
                }            // The source document's text length. If the length is over 3,
                // return.
                int oriLength = getLength();
                if (oriLength >= 3) {
                    return;
                }            // The length of the text to be inserted.
                int newLength = 0;
                char[] input = str.toCharArray();
                for (int i = 0; i < input.length; i++) {
                    char c = str.charAt(i);
                    // The whole length can't be over 3.
                    if (newLength + oriLength >= 3) {
                        break;
                    }
                    // Number only.
                    if (c <= '9' && c >= '0') {
                        input[newLength++] = c;
                    }
                }            // If sub IP is over 255, consume the typing.
                if (newLength + oriLength == 3) {
                    // Connect original string with new inputing string.
                    StringBuffer getStr = new StringBuffer();
                    char[] sourceChars = getText(0, oriLength).toCharArray();                // Append the source string before the inserting point.
                    for (int i = 0; i < offs; i++) {
                        getStr.append(sourceChars[i]);
                    }                // Append the input string.
                    for (int i = 0; i < newLength; i++) {
                        getStr.append(input[i]);
                    }                // Append the source string after the inserting point.
                    for (int i = offs; i < oriLength; i++) {
                        getStr.append(sourceChars[i]);
                    }                int ip;
                    try {
                        ip = Integer.parseInt(getStr.toString());
                    } catch (IllegalArgumentException iae) {
                        return;
                    }
                    // The ip cann't be over 255.
                    if (ip > 255) {
                        newLength--;
                    }
                }
                super.insertString(offs, new String(input, 0, newLength), a);
            }
        }
    }