gpt4 book ai didi

java - 当用户键入数字时,中间的四位数字显示为通配符

转载 作者:行者123 更新时间:2023-12-01 23:44:20 25 4
gpt4 key购买 nike

我正在开发一个 Swing 应用程序,它使用 JTextField 让用户输入电话号码。为了安全起见,当用户输入电话号码时,中间的四位数字需要显示为通配符。同时,当电话号码来自数据库时,此 JTextField 还会显示中间的四位数字作为通配符。

如何自定义JTextField?非常感谢任何帮助。

最佳答案

使用DocumentFilter

查看此示例,只需根据您的具体需求更改 new PhoneNumberFilter(6,10,'*') 即可。

F.I South Africa 的电话号码长度为 10 位数字,前 3 位是拨号代码,其余是唯一号码。

因此,如果希望用 *** 屏蔽最后 4 位数字,并且整个电话号码为 10 位数字,我会使用 new PhoneNumberFilter(6,10,'*') 来标记最后一个4(10-6=4)。

enter image description here

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;

public class Test {

public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

//create instance of our custom DocumentFiler class
PhoneNumberFilter phoneNumberFilter = new PhoneNumberFilter(6, 10, '*');


JTextField jtf = new JTextField(10);
//add filter to JTextField
((AbstractDocument) jtf.getDocument()).setDocumentFilter(phoneNumberFilter);
frame.add(jtf);

frame.pack();
frame.setVisible(true);

//jtf.setText("0119887654");
}
});
}

public static void main(String[] args) {
new Test();
}
}

class PhoneNumberFilter extends DocumentFilter {

private int textLength = 0;//keeps track of length of text within the field (used to check if we should start applying the mask)
private int numberMaskStartIndex;
private int numberMaskEndIndex;
private String mask;//what the characters in the specified ranges positions will be replaced with

public PhoneNumberFilter(int start, int end, char mask) {
numberMaskStartIndex = start;
numberMaskEndIndex = end - 1;
this.mask = String.valueOf(mask);
}

@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
if (string.length() > 1) {
for (int n = string.length() - 1; n >= 0; n--) {//an inserted string may be more than a single character i.e copy and pasting a number
char c = string.charAt(n);//get a single character of the string
if (n >= numberMaskStartIndex && n <= numberMaskEndIndex) {//check if its between the range which we should mask
super.replace(fb, i, i1, mask, as);
} else {
super.replace(fb, i, i1, String.valueOf(c), as);
}
textLength++;
}
} else if (textLength >= numberMaskStartIndex && textLength <= numberMaskEndIndex) {//only a singe character was inserted and its between the range which we should mask
super.replace(fb, i, i1, mask, as);
textLength++;
} else {
super.replace(fb, i, i1, string, as);
textLength++;
}
}

@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
super.remove(fb, i, i1);
if (i == 0 && i1 == textLength) {//if the text removed is the entire textfield i.e CTRL+A or Mouse dragged and DEL than we reset our counter which keeps track of the number of characters in the textfield
textLength = 0;
} else {//only a single character was deleted
textLength--;
}
}

@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
super.insertString(fb, i, string, as);
}
}

我没有添加限制用户输入的功能,因此可以输入任何内容,上面更多的是展示所需的逻辑:

For security, the four digit in the middle need to be displayed as wildcard while user type the phone number. Meanwhile, this JTextField also show the four digit in the middle as wildcard when the phone number is from database

您可能仍然会问从数据库获取时它是否有效,是的,因为 DocumentFilter 也适用于 setText(..);

关于java - 当用户键入数字时,中间的四位数字显示为通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17385881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com