我是 javaFx 新手,所以请忽略我这里的愚蠢问题我想验证(限制)用户输入 3 个字符(如果用户输入更多,则不应该允许输入,或者在 3 个字符之后,后面应该不可见)我发现了很多验证解决方案,但它不限制最多 3 个字符,而且理解起来几乎没有混乱 这是我的代码。
public class editController {
@FXML
private TextField countrycode;
public void add(ActionEvent event) {
String ADD=countrycode.getText();
try {
if(ADD.isEmpty()){
Alert alert=new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("Please Fill All DATA");
alert.showAndWait();
return;
}
FXMLLoader loader =new FXMLLoader();
loader.load(getClass().getResource("/region/newCountry.fxml").openStream());
使用 textProperty Listener 进行验证,例如:
countrycode.textProperty().addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (newValue.length() > 3) {
countrycode.setText(oldValue);
}
}
});
或获取字符串长度并检查类似
int length = ADD.length();
if (length > 3) {
System.out.println("Please Enter Lessthen 3 character");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句