英文:
How check radio buttons checked or not?
问题
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);
}
}
英文:
I have tried simple java programme using if statements. <br>
for that, I'm using radio buttons to check whether is check or not?<br>
please check my code below is not working properly someone please help me on this? <br>
Project link below<br>
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);
}
}
答案1
得分: 1
您代码的主要问题在于方法 BTN_submitActionPerformed 的第二行:
int age = Integer.parseInt(TXT_age.getText());
这里的 TXT_age 的值是 “Enter your age”。现在这无法解析为整数,因此会抛出 NumberFormatException,这会阻止程序继续执行预期的过程。该程序还会在单击时从此字段中移除占位文本,使其变为空,即 “”,因此提交它也会导致错误。
要解决上述问题,您可以将此方法重写如下:
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);
}
}
另外,您还可以考虑可能出现的其他问题。例如,用户可以在文本字段中输入任何内容,这意味着用户可以输入一个不适合int的数字。所以如果发生这种情况,您的程序将再次出错。然而,如果您对方法进行了上述更改,则您目前面临的问题将得到解决。
英文:
The main problem with your code is in the second line of your method BTN_submitActionPerformed
int age = Integer.parseInt(TXT_age.getText());
Here the value for TXT_age is “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. The program also removes the placeholder text from this field on click leaving it empty i.e “” 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. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论