gpt4 book ai didi

java - JTextField 事件的正则表达式不起作用?

转载 作者:行者123 更新时间:2023-12-02 10:00:26 27 4
gpt4 key购买 nike

我试图通过正则表达式避免在 JTextField 中输入无效数字,但是当我输入有效数字时,它也会转到 Consumer 方法来消费。请指导我。

  private void txtphoneKeyTyped(java.awt.event.KeyEvent evt) {                                  
char c = evt.getKeyChar();
String regex = "^(\\+93|0)?[7][0-9]{8}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(String.valueOf(c));
if(matcher.matches() == false){
evt.consume();
}
}

最佳答案

您不应该为此使用 KeyListener。

一方面,您尝试将单个键入的字符与整个电话号码模式进行匹配。 evt.getKeyChar() 返回单个字符的事实应该告诉您它不可能匹配整个电话号码。

正确的方法是使用 JTextField 的子类 JFormattedTextField ,而不是普通的 JTextField。

String regex = "^(\\+93|0)?7[0-9]{8}$";
Pattern pattern = Pattern.compile(regex);

JFormattedTextField.AbstractFormatter formatter =
new JFormattedTextField.AbstractFormatter() {
private static final long serialVersionUID = 1;

@Override
public Object stringToValue(String text)
throws ParseException {
if (text != null && !pattern.matcher(text).matches()) {
throw new ParseException(text, 0);
}
return text;
}

@Override
public String valueToString(Object value) {
return Objects.toString(value, null);
}
};

JTextField txtphone = new JFormattedTextField(formatter);

您实际上不需要限制键入的键;如果用户输入无效字符,该值将被拒绝,并且该字段将恢复为最后输入的有效值。但如果您坚持限制每个键入的字符,则可以使用 DocumentFilter :

Pattern allowedCharactersPattern = Pattern.compile("[0-9+]*");

DocumentFilter filter = new DocumentFilter() {
@Override
public void insertString(FilterBypass bypass,
int position,
String newText,
AttributeSet attr)
throws BadLocationException {
if (allowedCharactersPattern.matcher(newText).matches()) {
super.insertString(bypass, position, newText, attr);
}
}

@Override
public void replace(FilterBypass bypass,
int position,
int length,
String newText,
AttributeSet attr)
throws BadLocationException {
if (allowedCharactersPattern.matcher(newText).matches()) {
super.replace(bypass, position, length, newText, attr);
}
}
};

String regex = "^(\\+93|0)?7[0-9]{8}$";
Pattern pattern = Pattern.compile(regex);

JFormattedTextField.AbstractFormatter formatter =
new JFormattedTextField.AbstractFormatter() {
private static final long serialVersionUID = 1;

@Override
protected DocumentFilter getDocumentFilter() {
return filter;
}

@Override
public Object stringToValue(String text)
throws ParseException {
if (text != null && !pattern.matcher(text).matches()) {
throw new ParseException(text, 0);
}
return text;
}

@Override
public String valueToString(Object value) {
return Objects.toString(value, null);
}
};

JTextField txtphone = new JFormattedTextField(formatter);

您可能想知道为什么 allowedCharactersPattern 是 "[0-9+]*" 而不是完整的电话号码模式。这是因为使用带有可移动光标的文本字段的原因之一是用户可以一次输入一点信息,可能是无序的。例如,用户可以键入几个数字,然后使用箭头键将光标移动到开头并键入“+937”。在用户完成编辑之前最好不要强制执行模式。 stringToValue 方法仍然强制执行整体模式。

关于java - JTextField 事件的正则表达式不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55672184/

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