监听JPasswordField,假设我一次输入123456,除了6以外其余字符将会显示*,例如*****6,当用户继续输入时例如1234567 则显示******7
时间: 2023-11-14 14:12:51 浏览: 30
要监听JPasswordField,你需要使用addKeyListener()方法添加一个KeyListener。在keyPressed()方法中,你可以获取当前输入的字符并将其添加到一个字符串中。然后,你可以使用setEchoChar()方法将密码框中的所有字符替换为星号或其他字符。下面是一个示例代码:
```
JPasswordField passwordField = new JPasswordField();
passwordField.addKeyListener(new KeyAdapter() {
String password = "";
@Override
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
if (Character.isLetterOrDigit(c)) {
password += c;
}
passwordField.setText(password.replaceAll(".", "*"));
}
});
```
在这个示例中,我们使用了一个字符串变量password来存储用户输入的字符。在keyPressed()方法中,我们检查用户输入的字符是否是字母或数字,如果是,我们将其添加到password字符串中。然后,我们使用replaceAll()方法将密码框中的所有字符替换为星号。这样,除了最后一个字符以外,所有字符都会被隐藏。
相关问题
如何将密码框输入显示小圆点变为显示*
在大多数编程语言中,可以使用密码框控件的属性来设置输入显示的字符为小圆点或星号。具体实现方式可能会因编程语言和框架而异。以下是一些常见的实现方式:
在HTML和JavaScript中:
```html
<input type="password" id="myPasswordInput" />
<script>
var passwordInput = document.getElementById("myPasswordInput");
passwordInput.addEventListener("input", function() {
passwordInput.value = passwordInput.value.replace(/./g, "*");
});
</script>
```
在Java中:
```java
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
```
在C#中:
```csharp
TextBox passwordBox = new TextBox();
passwordBox.PasswordChar = '*';
```
在Python中(使用Tkinter库):
```python
from tkinter import *
root = Tk()
passwordEntry = Entry(root, show='*')
passwordEntry.pack()
root.mainloop()
```
Jtextfiled只显示后一位,其它用*代替,例如****1
您可以使用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);
}
}
});
```
这样,当用户输入文本时,文本框中只会显示最后一个字符,其他字符都用星号代替。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)