我想强制文本字段只接受字母和空格。 我试过这个看起来不错的功能,但它只打印出来:
接受,i = 0所以我可以看到它是局部变量或类似smthg的问题;
public void ff()
{Boolean bool=true;
int i=0;
name=textField.getText();
while(bool && i<name.length())
{
if(Character.isLetter(name.charAt(i)))
i++;
else
bool=false;
}
if (bool=true)
System.out.println("accepted"+i);
else
System.out.println("wrong"+i);
}
你能帮我解决一下吗?并且我想使用此函数而不是documentfilter或formattedtext。
答案 0 :(得分:0)
bool=true
是作业,而不是比较。
将if (bool=true)
更改为if (bool==true)
或if (bool)
。否则,您将始终打印accepted
。