Java JTextField中的异常处理
我正在写一份餐馆申请书 在这一部分中,为了添加cook,用户需要在nameTextField中输入cook的姓名,在salaryTextField中输入cook的薪水,在name部分,我想阻止用户输入数字,在salary部分,我想阻止用户输入单词。对于薪资部分,我尝试使用异常处理,但没有成功Java JTextField中的异常处理,java,user-interface,exception,Java,User Interface,Exception,我正在写一份餐馆申请书 在这一部分中,为了添加cook,用户需要在nameTextField中输入cook的姓名,在salaryTextField中输入cook的薪水,在name部分,我想阻止用户输入数字,在salary部分,我想阻止用户输入单词。对于薪资部分,我尝试使用异常处理,但没有成功 class AddButtonInCookClick implements ActionListener{ public void actionPerformed(Acti
class AddButtonInCookClick implements ActionListener{
public void actionPerformed(ActionEvent e) {
String name = (String)nameTextFieldCook.getText();
double salary = new Double(0.0);
try {
salary = Double.parseDouble(salaryTextField.getText());
}catch(NumberFormatException ex) {
System.out.println(ex);
}
restaurant.getEmployees().add(new Cook(id, name, salary));
id++;
JOptionPane cookOptionPane = new JOptionPane();
JOptionPane.showMessageDialog(cookOptionPane, "Cook added succesfully.");
}
}
addButtonInCook.addActionListener(new AddButtonInCookClick());
即使程序没有崩溃。我仍然无法让用户为工资部分输入数字。谢谢您的帮助。您可以使用代替正常的
JTextField
将输入仅限于数字
示例代码:
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import java.text.NumberFormat;
import javax.swing.text.NumberFormatter;
public class Test extends JFrame
{
JFormattedTextField salaryFormattedTextField;
NumberFormat numberFormat;
NumberFormatter numberFormatter;
public Test()
{
numberFormat = NumberFormat.getInstance();
// delete line if you want to see commas or periods grouping numbers based on your locale
numberFormat.setGroupingUsed(false);
numberFormatter = new NumberFormatter(format);
numberFormatter.setValueClass(Integer.class);
// delete line if you want to allow user to enter characters outside the value class.
// Deleting the line would allow the user to type alpha characters, for example.
// This pretty much defeats the purpose of formatting
numberFormatter.setAllowsInvalid(false);
salaryFormattedTextField = new JFormattedTextField(formatter);
this.add(salaryFormattedTextField);
}
public static void main(String[] args)
{
Test test = new Test();
s.pack();
s.setVisible(true);
}
}
另一种方法是,使用您已有的代码结构,在输入没有正确解析时抛出一个JOptionPane
try
{
salary = Double.parseDouble(salaryTextField.getText());
restaurant.getEmployees().add(new Cook(id, name, salary));
id++;
JOptionPane cookOptionPane = new JOptionPane();
JOptionPane.showMessageDialog(cookOptionPane, "Cook added succesfully.");
}
catch(NumberFormatException ex)
{
JOptionPane cookFailPane = new JOptionPane();
JOptionPane.showMessageDialog(cookFailPane , "Could not add cook. Please enter salary using only numeric input.");
ex.printStackTrace();
}
您可以使用代替正常的
JTextField
将输入仅限于数字
示例代码:
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import java.text.NumberFormat;
import javax.swing.text.NumberFormatter;
public class Test extends JFrame
{
JFormattedTextField salaryFormattedTextField;
NumberFormat numberFormat;
NumberFormatter numberFormatter;
public Test()
{
numberFormat = NumberFormat.getInstance();
// delete line if you want to see commas or periods grouping numbers based on your locale
numberFormat.setGroupingUsed(false);
numberFormatter = new NumberFormatter(format);
numberFormatter.setValueClass(Integer.class);
// delete line if you want to allow user to enter characters outside the value class.
// Deleting the line would allow the user to type alpha characters, for example.
// This pretty much defeats the purpose of formatting
numberFormatter.setAllowsInvalid(false);
salaryFormattedTextField = new JFormattedTextField(formatter);
this.add(salaryFormattedTextField);
}
public static void main(String[] args)
{
Test test = new Test();
s.pack();
s.setVisible(true);
}
}
另一种方法是,使用您已有的代码结构,在输入没有正确解析时抛出一个JOptionPane
try
{
salary = Double.parseDouble(salaryTextField.getText());
restaurant.getEmployees().add(new Cook(id, name, salary));
id++;
JOptionPane cookOptionPane = new JOptionPane();
JOptionPane.showMessageDialog(cookOptionPane, "Cook added succesfully.");
}
catch(NumberFormatException ex)
{
JOptionPane cookFailPane = new JOptionPane();
JOptionPane.showMessageDialog(cookFailPane , "Could not add cook. Please enter salary using only numeric input.");
ex.printStackTrace();
}
我相信你需要一个
按键监听器
,在每次按键时检查输入并立即显示错误。来到您的代码,您只是记录异常。这不会阻止用户输入字母表。您有几种方法可以做到这一点,包括但不限于:、和。@sunildaburi:这是一个非常糟糕的建议。最好还是按照阿布拉的建议去做,因为它们更安全、更坚固。当用户将数据复制并粘贴到文本字段中时,KeyListener将如何工作?您知道使用KeyListener有可能导致JTextField工作异常吗?同样,不要建议这样做。有关您为什么应该避免使用KeyListener以及为什么应该遵循@Abra的建议的更多信息,请阅读。我相信您需要一个KeyListener
,并在每次按键时检查输入并立即显示错误。来到您的代码,您只是记录异常。这不会阻止用户输入字母表。您有几种方法可以做到这一点,包括但不限于:、和。@sunildaburi:这是一个非常糟糕的建议。最好还是按照阿布拉的建议去做,因为它们更安全、更坚固。当用户将数据复制并粘贴到文本字段中时,KeyListener将如何工作?您知道使用KeyListener有可能导致JTextField工作异常吗?同样,不要推荐这个。更多关于为什么你应该避免使用KeyListener和为什么你应该遵循@Abra的建议,请阅读。