如果输入不是想要的,如何弹出重试?
How do I make a retry pop up if the input isn't what is wanted?
所以当用户没有在 JTextField 中输入 0-60 的数字时,我试图制作一个 JOptionPanel 弹出窗口,但我不确定如何
这是我试过的
else if(e.getSource() == Save) {
Task task = new Task();
task.hour = hin.getText();
task.min = min.getText();
if(min.getText() > 0 && min.getText() < 60) {
JOptionPane.showMessageDialog(null, "Please enter a valid time.");
}
else{
task.day = (String) dayspinner.getValue();
task.description = desc.getText();
tasks.add(task);
ObjectOutputStream os = null;
try{
os = new ObjectOutputStream(new FileOutputStream(save));
os.writeObject(tasks);
os.close();
}catch(IOException ex) {
ex.printStackTrace();
}
tframe.setVisible(false);
}
}
但是我得到的错误是 "bad operand types for binary operator'<'"。
getText()
方法 returns 和 String
,并且不能使用 <
或 >
来比较 String
。如果先将 min#getText
的结果转换为 int
的结果,然后再进行适当的比较会更好。您可以使用 Integer#parseInt()
:
int minVal = Integer.parseInt(min.getText());
//fixed the condition
if (!(minVal > 0 && minVal < 60)) {
//rest of the code...
}
String
无法使用比较运算符进行比较。改为解析 int
值 (Integer.parseInt(String someString)
) 并进行比较。
对于用户输入验证,请使用 JFormattedTextField
、JSpinner
、InputVerfifer
甚至 DocumentFilter
(但如果您到达这里,您可能做错了)
这些将允许您实时验证字段(post 焦点更改)并确保只允许用户输入您想要的值。您还可以使用 InputVerifier
来限制焦点的更改,这样用户在输入有效值之前将无法离开该字段
有关详细信息,请参阅 How to Use Formatted Text Fields, How to Use Spinners and Validating Input。
改变
if(min.getText() > 0 && min.getText() < 60) {
JOptionPane.showMessageDialog(null, "Please enter a valid time.");
}
至
if(Integer.parseInt(min.getText()) > 0 && Integer.parseInt(min.getText()) < 60) {
JOptionPane.showMessageDialog(null, "Please enter a valid time.");
}
所以当用户没有在 JTextField 中输入 0-60 的数字时,我试图制作一个 JOptionPanel 弹出窗口,但我不确定如何
这是我试过的
else if(e.getSource() == Save) {
Task task = new Task();
task.hour = hin.getText();
task.min = min.getText();
if(min.getText() > 0 && min.getText() < 60) {
JOptionPane.showMessageDialog(null, "Please enter a valid time.");
}
else{
task.day = (String) dayspinner.getValue();
task.description = desc.getText();
tasks.add(task);
ObjectOutputStream os = null;
try{
os = new ObjectOutputStream(new FileOutputStream(save));
os.writeObject(tasks);
os.close();
}catch(IOException ex) {
ex.printStackTrace();
}
tframe.setVisible(false);
}
}
但是我得到的错误是 "bad operand types for binary operator'<'"。
getText()
方法 returns 和 String
,并且不能使用 <
或 >
来比较 String
。如果先将 min#getText
的结果转换为 int
的结果,然后再进行适当的比较会更好。您可以使用 Integer#parseInt()
:
int minVal = Integer.parseInt(min.getText());
//fixed the condition
if (!(minVal > 0 && minVal < 60)) {
//rest of the code...
}
String
无法使用比较运算符进行比较。改为解析 int
值 (Integer.parseInt(String someString)
) 并进行比较。
对于用户输入验证,请使用 JFormattedTextField
、JSpinner
、InputVerfifer
甚至 DocumentFilter
(但如果您到达这里,您可能做错了)
这些将允许您实时验证字段(post 焦点更改)并确保只允许用户输入您想要的值。您还可以使用 InputVerifier
来限制焦点的更改,这样用户在输入有效值之前将无法离开该字段
有关详细信息,请参阅 How to Use Formatted Text Fields, How to Use Spinners and Validating Input。
改变
if(min.getText() > 0 && min.getText() < 60) {
JOptionPane.showMessageDialog(null, "Please enter a valid time.");
}
至
if(Integer.parseInt(min.getText()) > 0 && Integer.parseInt(min.getText()) < 60) {
JOptionPane.showMessageDialog(null, "Please enter a valid time.");
}