我想使用JTextField
方法验证我的其他InputVerifier
。我做了什么我使用JTextField
为其他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
字符串是否等于"tfAddress"
但不幸的是它不符合条件。任何帮助或提示如何解决这个问题?
答案 0 :(得分:0)
在您的代码中,此语句if(text.equals("") && name.equals("tfAddress"))
永远不会满足,因为if(text.trim().length() == 0 || text.equals(""))
检查,因此text.equals("")
永远不会返回true,因此name.equals("tfAddress")
将跳过。
在if
子句的第一次检查中,如果文本为空,则代码将返回。因此,if(text.equals("") && name.equals("tfAddress"))
if(name.equals("tfAddress"))
可以检查dom4j-1.6.1.jar
poi-3.11.jar
poi-ooxml-3.11.jar
poi-ooxml-schemas-3.15-beta2.jar
xmlbeans-2.6.0.jar
poi-scratchpad-3.11.jar
poi-excelant-3.11.jar
答案 1 :(得分:0)
我刚刚解决了这个问题。我在逻辑上犯了一个错误。我基于text.trim().length() == 0 || text.equals("")
所以当程序运行时,首先检查文本是否为空。我做了什么我根据setName
方法设置条件。希望这会对其他人有所帮助。
private void validateJTextField()
{
tfLastName.setName("tfLastName");
tfFirstName.setName("tfFirstName");
tfMiddleName.setName("tfMiddleName");
tfNickname.setName("tfNickname");
tfAddress.setName("tfAddress");
tfContact.setName("tfContact");
tfLastName.setInputVerifier(new Validation());
tfFirstName.setInputVerifier(new Validation());
tfMiddleName.setInputVerifier(new Validation());
tfNickname.setInputVerifier(new Validation());
tfAddress.setInputVerifier(new Validation());
tfContact.setInputVerifier(new Validation());
}
public class Validation extends InputVerifier
{
@Override
public boolean verify(JComponent input)
{
String text = null;
String cb = null;
String name = input.getName();
if(input instanceof JTextField)
{
text = ((JTextField) input).getText();
if(name.equals("tfLastName") || name.equals("tfFirstName") || name.equals("tfMiddleName") || name.equals("tfNickname"))
{
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)
{
}
}
}
else if(name.equals("tfAddress"))
{
if(text.trim().length() == 0 || text.equals(""))
{
JOptionPane.showMessageDialog(null, "Cannot left blank");
return false;//Return false if the component need to keep focus
}
}
}