验证jtextfield Java的问题

时间:2018-03-30 14:30:53

标签: java swing validation jtextfield

昨天它工作,除非我忘了保存我的更改,我无法弄清楚它有什么问题。我希望它允许数字,逗号和句号。

textField_FlightPrice = new JTextField();
        textField_FlightPrice.setColumns(10);
        textField_FlightPrice.setBounds(152, 291, 273, 22);
        contentPane.add(textField_FlightPrice);
        textField_FlightPrice.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent g) {
                // allows only numbers, , ., back space, delete, and slash.
                char c = g.getKeyChar();
                if ((c < '0') || (c > '9') && (c != ',') && (c != '.') && (c != KeyEvent.VK_BACK_SPACE) && (c != KeyEvent.VK_DELETE)
                        && (c != KeyEvent.VK_SLASH)) {
                    g.consume(); // ignore event
                }
            }

        });

0 个答案:

没有答案