【发布时间】:2025-11-28 02:40:01
【问题描述】:
我正在尝试在课堂上为我的 GUI 编写一些验证代码。我将如何将 JTextField 中的文本放入 while 语句并提示 JOptionPane 让用户输入必要的数字(双精度)?更具体地说,如何检查我从 JTextField 得到的内容是否是字符串/字符串+数字/除数字之外的任何其他内容?
String text=JTextField.getText();
while(text.equals*a string or anything but a number*);
JOP("Invalid input ............ etc...
如果你有时间,这里是我的 GUI 和我的课程。我正在尝试对其余方法执行此操作。但是上面的答案就足够了。
http://www.mediafire.com/?f079i1xtihypg1b
http://www.mediafire.com/file/f079i1xtihypg1b/FinanceGUI.java
更新:
这是我目前所拥有的:
//get the text entered in the amountRentText
//JTextField and parse it to a Double
String amtRentIn=amountRentText.getText();
try{Double.parseDouble(amtRentIn);}
catch(NumberFormatException nfe){
while()
amtRentIn=JOptionPane.showInputDialog("Invalid input. Please "+
"enter your rent: ");
}
double rent= Double.parseDouble(amtRentIn);
fin.setRent(rent);
我在这段时间里放了什么?
【问题讨论】:
标签: java validation user-interface input