我想验证我的其他JTextField
使用InputVerifier
方法。我做了什么,我为不同的JTextField
using设置了一个名称setName
。
private void validateJTextField()
{
tfAddress.setName("tfAddress");
tfLastName.setInputVerifier(new Validation());
tfFirstName.setInputVerifier(new Validation());
tfMiddleName.setInputVerifier(new Validation());
tfNickname.setInputVerifier(new Validation());
tfAddress.setInputVerifier(new Validation());
}
验证类
public class Validation extends InputVerifier
{
@Override
public boolean verify(JComponent input)
{
String text = null;
String name = input.getName();
if(input instanceof JTextField)
{
text = ((JTextField) input).getText();
if(text.trim().length() == 0 || text.equals(""))
{
JOptionPane.showMessageDialog(null, "Cannot left blank");
return false;//Return false if the component need to keep focus
}
else
{
try
{
Double.parseDouble(text);
JOptionPane.showMessageDialog(null, "Cannot insert numeric");
return false;
}
catch(NumberFormatException e)
{
}
}
if(text.equals("") && name.equals("tfAddress"))
{
System.out.print("This is tfAddress");
return false;
}
}
return true;//Return true if the component should give up focus
}
}
正如您在此处看到的,我正在尝试验证或检查name
String 是否等于,"tfAddress"
但不幸的是它不满足条件。任何帮助或提示我该如何解决这个问题?