Jtextfiled只显示后一位,其它用*代替,例如****1
时间: 2023-11-14 20:12:51 浏览: 49
您可以使用JPasswordField类来实现这个功能。JPasswordField是JTextField的子类,它可以隐藏输入的字符。您可以使用setEchoChar(char)方法来设置掩码字符,例如:
```
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
```
这样,当用户输入密码时,所有字符都会被替换成星号。如果您只想显示最后一个字符,可以使用DocumentListener来监听文本框的变化,并在文本框中只显示最后一个字符,其他字符用星号代替。具体实现可以参考以下代码:
```
JTextField textField = new JTextField();
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
updateText();
}
@Override
public void removeUpdate(DocumentEvent e) {
updateText();
}
@Override
public void changedUpdate(DocumentEvent e) {
updateText();
}
private void updateText() {
String text = textField.getText();
if (text.length() > 1) {
String lastChar = text.substring(text.length() - 1);
String maskedText = "****" + lastChar;
textField.setText(maskedText);
}
}
});
```
这样,当用户输入文本时,文本框中只会显示最后一个字符,其他字符都用星号代替。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)