简体   繁体   English

如何检查单选按钮是否选中?

[英]How check radio buttons checked or not?

I have tried simple java programme using if statements.我尝试过使用 if 语句的简单 java 程序。
for that, I'm using radio buttons to check whether is check or not?为此,我使用单选按钮来检查是否选中?
please check my code below is not working properly someone please help me on this?请检查我下面的代码是否正常工作有人请帮助我吗?
Project link below项目链接如下
https://drive.google.com/file/d/1xhNbKyXYJh2k5i7a6nVl1VkTY7dTNzSg/view?usp=sharing https://drive.google.com/file/d/1xhNbKyXYJh2k5i7a6nVl1VkTY7dTNzSg/view?usp=sharing

private void BTN_submitActionPerformed(java.awt.event.ActionEvent evt) {                                           
        String finalResult;
        int age = Integer.parseInt(TXT_age.getText());
        RBTN_male.setActionCommand("male");
        RBTN_female.setActionCommand("female");
        String radio_Result = BTNgrp_gender.getSelection().getActionCommand();


        if (RBTN_male.isSelected() || RBTN_female.isSelected()) {
            if (TXT_age.getText() == null || TXT_age.getText().trim().isEmpty()) {

                JOptionPane optionPane = new JOptionPane("Please fill your age", JOptionPane.ERROR_MESSAGE);
                JDialog dialog = optionPane.createDialog("Age missing");
                dialog.setAlwaysOnTop(true);
                dialog.setVisible(true);

            } else {
                if (age == 0) {
                    JOptionPane optionPane = new JOptionPane("Please enter valid age", JOptionPane.ERROR_MESSAGE);
                    JDialog dialog = optionPane.createDialog("Wrong age");
                    dialog.setAlwaysOnTop(true);
                    dialog.setVisible(true);
                } else {
                    if (age > 0 && age <= 5) {
                        finalResult = "Rhymes";
                        LBL_result.setText(finalResult);
                    } else if (age > 15) {
                        finalResult = "Poetry";
                        LBL_result.setText(finalResult);
                    }
                }
            }

        } else {
            JOptionPane optionPane = new JOptionPane("Please Select Your Gender", JOptionPane.ERROR_MESSAGE);
            JDialog dialog = optionPane.createDialog("Gender Missing");
            dialog.setAlwaysOnTop(true);
            dialog.setVisible(true);
        }


    }                                          

The main problem with your code is in the second line of your method BTN_submitActionPerformed您的代码的主要问题是在您的方法BTN_submitActionPerformed的第二行

int age = Integer.parseInt(TXT_age.getText()); 

Here the value for TXT_age is “Enter your age” .这里TXT_age的值是“Enter your age” Now this cannot be parsed to an integer therefore a NumberFormatException is thrown which prevents the program from continuing its expected course of execution.现在无法将其解析为 integer,因此会抛出NumberFormatException ,从而阻止程序继续其预期的执行过程。 The program also removes the placeholder text from this field on click leaving it empty ie “” therefore submitting it will also result in an error.该程序还会在单击时从此字段中删除占位符文本,将其留空,即“” ,因此提交它也会导致错误。

To solve the issues above you can rewrite this method as follows:要解决上述问题,您可以按如下方式重写此方法:

    private void BTN_submitActionPerformed(java.awt.event.ActionEvent evt) {
        int age;
        String finalResult;
        JOptionPane optionPane;
        JDialog dialog;
        RBTN_male.setActionCommand("male");
        RBTN_female.setActionCommand("female");
        try {
            age = Integer.parseInt(TXT_age.getText());
            if (RBTN_male.isSelected() || RBTN_female.isSelected()) {

                if (age == 0  || age < 0 ) {
                    optionPane = new JOptionPane("Please enter valid age", JOptionPane.ERROR_MESSAGE);
                    dialog = optionPane.createDialog("Wrong age");
                    dialog.setAlwaysOnTop(true);
                    dialog.setVisible(true);
                } else {
                    if (age > 0 && age <= 5) {
                        finalResult = "Rhymes";
                        LBL_result.setText(finalResult);
                    } else if (age > 15) {
                        finalResult = "Poetry";
                        LBL_result.setText(finalResult);
                    }
                }

            } else {
                optionPane = new JOptionPane("Please Select Your Gender", JOptionPane.ERROR_MESSAGE);
                dialog = optionPane.createDialog("Gender Missing");
                dialog.setAlwaysOnTop(true);
                dialog.setVisible(true);
            }
        } catch (NumberFormatException e) {
            optionPane = new JOptionPane("Please fill your age", JOptionPane.ERROR_MESSAGE);
            dialog = optionPane.createDialog("Age missing");
            dialog.setAlwaysOnTop(true);
            dialog.setVisible(true);
        }

    }

On a sidenote you can also think of other problems that may arise.在旁注中,您还可以考虑可能出现的其他问题。 For example, as user can enter anything in the textfield that means a user can enter a number that may not fit in an int.例如,由于用户可以在文本字段中输入任何内容,这意味着用户可以输入一个可能不适合 int 的数字。 So if that happens then your program will again break.因此,如果发生这种情况,您的程序将再次中断。 However, if you make above mentioned changes to your method then the problems that you are facing currently will be resolved.但是,如果您对方法进行上述更改,那么您当前面临的问题将得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM