Java JTextField警告问题

时间:2015-02-13 07:08:44

标签: java try-catch warnings jtextfield

我正在制作一个java welcome GUI。用户输入他的名字,姓氏,年龄,性别,它将显示给他。如果用户没有输入除名字和姓氏之外的字段,我会设置警告。它会针对年龄,状态和性别发出警告,但会跳过名称的警告并转到try / catch。我不明白为什么会这样做,如何修复它以显示警告?

buttonWelcome.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent pressing) {

            int userAge;

            // a try/catch to catch any error and display
            // back to the user that he must finish
            try {

                String first = firstname.getText();
                String last = lastname.getText();
                Object state = stateBox.getSelectedItem();
                userAge = Integer.parseInt(age.getText());
                boolean male = maleButton.isSelected();
                boolean female = femaleButton.isSelected();

                // warning section

                if (firstname.getText().trim().equals("")) {

                    JOptionPane.showMessageDialog(null,
                            "Must enter first name");

                } else if (lastname.getText().trim().equals("")) {

                    JOptionPane.showMessageDialog(null,
                            "Must enter last name");

                } else if (userAge <= 0) {

                    JOptionPane.showMessageDialog(null,
                            "Must be a positive age");

                } else if (stateBox.getSelectedIndex() == 0) {

                    JOptionPane
                            .showMessageDialog(null, "Must select state");

                } else if (buttonGenders.isSelected(null)) {

                    JOptionPane.showMessageDialog(null,
                            "Must select gender");

                }

                if (male) {

                    textArea.setText("Welcome Male " + first + ", " + last
                            + " of " + userAge + " years old from " + state);

                } else if (female) {

                    textArea.setText("Welcome Female " + first + ", "
                            + last + " of " + userAge + " years old from "
                            + state);

                }

                // display back the error as a pop up
            } catch (Exception e) {

                JOptionPane.showMessageDialog(null,
                        "Must Complete your Info");

            }

        }
    });

0 个答案:

没有答案