伙计们我试着去使用JOptionPane的,但是,如果我输入了错误的输入值,并且不退出PROGRAMM了取消按钮的响应。 任何想法是对子级非常有用的!
int n = 0, k = 0;
Students stu = new Students();
while (n <= 0) {
try {
n = Integer.parseInt(JOptionPane.showInputDialog(stu, "Enter the number of people","Input", JOptionPane.INFORMATION_MESSAGE));
if (n <= 0) {
OptionPane.showMessageDialog(stu, "You have given a wrong input!",
"Warning", JOptionPane.WARNING_MESSAGE);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(stu, "You have given a wrong input!",
"Warning", JOptionPane.WARNING_MESSAGE);
n = 0;
}
}
这是你想要的东西:
int n;
String code = JOptionPane.showInputDialog(null,
"Enter the size of the group",
"Team Combination Finder",
JOptionPane.INFORMATION_MESSAGE);
if (code == null) {
System.out.println("This is cancel button");
System.exit(0);
} else if (code.equalsIgnoreCase("")) {
System.out.println("This is OK button without input");
} else {
try {
n = Integer.parseInt(code);
if (n <= 0) {
System.out.println("This is wrong input");
} else {
System.out.println("This is right input");
}
} catch (Exception e) {
System.out.println("You must input numeric only");
}
}
看看它是否适合你:)
作为一个方面说明,我认为拼写错误JOptionPane
(第11行)应该不必要引发异常。
你已经证明了OptionDialog后,你必须使用一个break语句,为了打破循环。 JOptionPane的不知道你的循环。
或者使用
System.exit(ErrorCode);
代替break;
更新
所以我觉得你想要的是这样的:
String input = JOptionPane.showInputDialog(null, "Enter name : ", "New Record!",
while (n <= 0) {
try {
String input = JOptionPane.showInputDialog(stu, "Enter the number of people","Input", JOptionPane.INFORMATION_MESSAGE);
if(input == null || input.length() == 0)
{
OptionPane.showMessageDialog(stu, "You have given a wrong input!",
"Warning", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
n = Integer.parseInt(input);
if (n <= 0) {
OptionPane.showMessageDialog(stu, "You have given a wrong input!",
"Warning", JOptionPane.WARNING_MESSAGE);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(stu, "You have given a wrong input!",
"Warning", JOptionPane.WARNING_MESSAGE);
n = 0;
}
}