连续循环验证
我目前正在尝试找出一种方法,如何将验证循环到按下按钮时的位置,它将验证 JTextfield 输入,然后在按下“确定”后在对话框中打印一些内容,它将继续循环此验证,直到达到 times
的值。我假设我会使用 for 循环,但我真的很困惑它的输入位置。当它达到时间值后,我只需 frame.setVisible(false)
并隐藏它。
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String txtField = testTextField.getText();
int times = 3;
if (firstNameInvalid(txtField) == false) {
JOptionPane.showMessageDialog(null, "input valid text field");
} else {
JOptionPane.showMessageDialog(null, "gratz, onto the next !!");
}
}
});
I am currently trying to figure out a way how to loop a validation to where when a button is pressed, it will validate the JTextfield
inputs then print something in a dialog in which after "OK" is pressed, it will keep cycling this validation till it hits the value of times
. I assume I would use a for loop but I am genuinely confused on where it would be inputted. After it has reached the value of the times, I would just frame.setVisible(false)
and hide it.
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String txtField = testTextField.getText();
int times = 3;
if (firstNameInvalid(txtField) == false) {
JOptionPane.showMessageDialog(null, "input valid text field");
} else {
JOptionPane.showMessageDialog(null, "gratz, onto the next !!");
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不应该。虽然此解决方案适用于线性控制台程序,但对于事件驱动的 GUI 代码来说这不是一个可行的解决方案。
相反,创建状态字段、变量,在给定输入时更改它们,并将程序的响应基于这些状态字段所保存的值。
然后你可以想象创建一个
List; nameList = new ArrayList<>();
您的关键变量,此列表的.size()
是您的程序遵循的状态。每次激活 ActionListener 时,它都会从 JTextField 获取文本并将其添加到 nameList 中。然后它检查
nameList.size()
是否等于您的times
变量,您就完成了,然后需要在完成任务时执行所需的任何处理。但同样,for 循环在这里没有帮助,因为您实际上并没有循环。如果您使用 for 循环,就像获取扫描仪输入时一样,您将通过阻止其事件线程来冻结 GUI。事件驱动程序中的所有内容都应该由事件驱动:监听事件并响应事件。
No, you shouldn't. While this solution works for linear console programs, this is not a viable solution for event driven GUI code.
Instead, create state fields, variables, that you change as input is given, and base the program's response to values held by these state fields.
Then you could conceivably make an
List<String> nameList = new ArrayList<>();
your key variable, and the.size()
of this list the state that your program follows.Each time the ActionListener is activated, it gets the text from the JTextField and adds it to the nameList. Then it checks the
nameList.size()
if equal to yourtimes
variable, you're done, and then need to do whatever processing is required on completion of the task.But again, a for-loop is not helpful here since you're not actually looping. If you used a for loop, like you would when getting Scanner input, you'd freeze the GUI by blocking its event thread. Everything in an event-driven program should be driven by, well, events: listen to events and respond to events.
跟踪验证输入的次数,与需要循环的次数进行比较,根据需要做出决策
Keep track of the number of times you've validated the input, compare to the number of times you need to loop, make decisions as required