0

如何为用户设置输入限制,我的意思是用户只能输入 2 或 4 个整数,不能再输入了。

代码:

 JLabel dateD = new JLabel("| Date  Day:");
 dateD.setBounds(170,270, 120, 25);

 dateDD = new JTextField();
 dateDD.setBounds(235,270, 20, 25);
4

2 回答 2

1

使用DocumentFilter应该对您进行分类,按如下方式创建过滤器:-

class MaximumCharacters extends DocumentFilter {

        private int maxLength;

        public MaximumCharacters() {
            maxLength = 10; // The number of characters allowed
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String string,
                AttributeSet attr) throws BadLocationException {
            if (maxLength > 0
                    && fb.getDocument().getLength() + string.length() <= maxLength) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length,
                String text, AttributeSet attrs) throws BadLocationException {

            if ((fb.getDocument().getLength() + text.length() - length) <= maxLength)
                super.replace(fb, offset, length, text, attrs);
        }
    }

然后您将 设置DocumentFilter为您的JTextField组件,如下所示:-

((AbstractDocument) dateDD.getDocument()).setDocumentFilter(new MaximumCharacters());
于 2013-03-26T18:13:35.287 回答
0

对于上面的答案,下面是我为处理上述情况而创建的实用方法 -

    /**
     * Method installNumericMaximumCharacters.
     * 
     * @param document
     *            AbstractDocument
     * @param numberofChars
     *            int
     */
    public static void installNumericMaximumCharacters(
            AbstractDocument document, final int numberofChars) {
        document.setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset,
                    String string, AttributeSet attr)
                    throws BadLocationException {
                try {
                    if (string.equals(".")
                            && !fb.getDocument()
                                    .getText(0, fb.getDocument().getLength())
                                    .contains(".")) {
                        super.insertString(fb, offset, string, attr);
                        return;
                    }
                    Double.parseDouble(string);
                    if (fb.getDocument().getLength() + string.length() <= numberofChars) {
                        super.insertString(fb, offset, string, attr);
                    } else {
                        Toolkit.getDefaultToolkit().beep();
                    }
                } catch (Exception e) {
                    Toolkit.getDefaultToolkit().beep();
                }
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length,
                    String text, AttributeSet attrs)
                    throws BadLocationException {
                try {
                    if (text.equals(".")
                            && !fb.getDocument()
                                    .getText(0, fb.getDocument().getLength())
                                    .contains(".")) {
                        super.insertString(fb, offset, text, attrs);
                        return;
                    }
                    Double.parseDouble(text);
                    int l = fb.getDocument().getLength() - length
                            + text.length();
                    if (length > 0) {
                        fb.getDocument().remove(offset, length);
                    }
                    if (l <= numberofChars) {
                        super.insertString(fb, offset, text, attrs);

                    } else {
                        Toolkit.getDefaultToolkit().beep();
                    }
                } catch (Exception e) {
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        });
    }

该方法有点不言自明。当它发生变化时,Document它会唤起DocumentFilter. 请注意,您不能setText()相同Document。您将不得不为此使用该FilterBypass对象。

于 2013-03-26T18:39:37.247 回答