如何在javafx密码字段中输入IME

时间:2017-11-24 11:42:03

标签: javafx passwords ime

在网页中,可以设置密码字段以禁用microsoft IME以防止用户输入其他语言字符。例如,中文或日本字符不能在该字段中输入。

但在javafx UI中,我很容易将中文字符放入密码字段。谁知道如何在密码输入中输入字符时禁用微软IME?我只想在密码字段中输入数字,符号和英文字符。

1 个答案:

答案 0 :(得分:1)

如果您真的想限制可用于密码的字符范围,可以测试一下:

Pattern pattern = Pattern.compile("[a-zA-Z0-9\\-\\+]*");

PasswordField passwordField = new PasswordField();
passwordField.setTextFormatter(new TextFormatter<String>(change -> {
    if(pattern.matcher(change.getText()).matches()) {
        return change;
    }

    return null;
}));