gpt4 book ai didi

java - 如何使文本字段有 2 个文档过滤器

转载 作者:搜寻专家 更新时间:2023-10-31 20:21:59 26 4
gpt4 key购买 nike

我这里有一个代码,是我从 MDP 的博客中获得的。大小过滤器和数字过滤器。我如何让文本字段为两个文档过滤器设置过滤器。

这是数字过滤器

import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;
import javax.swing.text.DocumentFilter;

public class IntFilter extends DocumentFilter {

public void insertString(DocumentFilter.FilterBypass fb, int offset,
String string, AttributeSet attr)
throws BadLocationException {

StringBuffer buffer = new StringBuffer(string);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch)) {
buffer.deleteCharAt(i);
}
}
super.insertString(fb, offset, buffer.toString(), attr);
}

public void replace(DocumentFilter.FilterBypass fb,
int offset, int length, String string, AttributeSet attr) throws BadLocationException {
if (length > 0) fb.remove(offset, length);
insertString(fb, offset, string, attr);
}
}

此代码用于大小过滤器

import java.awt.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class SizeFilter extends DocumentFilter {

private int maxCharacters;

public SizeFilter(int maxChars) {
maxCharacters = maxChars;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
super.insertString(fb, offs, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()
- length) <= maxCharacters)
super.replace(fb, offs, length, str, a);
else
Toolkit.getDefaultToolkit().beep();
}
}

最佳答案

据我所知,您有两个选择。创建一个迭代每个过滤器的复合过滤器:

public class CompositeFilter extends DocumentFilter {
private final DocumentFilter[] filters;

public CompositeFilter(DocumentFilter... filters) {
this.filters = filters;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {
for (DocumentFilter filter : filters) {
filter.insertString(fb, offs, str, a);
}
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
throws BadLocationException {
for (DocumentFilter filter : filters) {
filter.replace(fb, offs, length, a);
}
}
}

您可能希望首先使用限制性更强的过滤器来实例化复合 Material ,因此您可以像这样构建它:

new CompositeFilter(new SizeFilter(10), new IntFilter());

如果顺序非常重要,您可以考虑将过滤器重写为装饰器,例如将第二个过滤器传递给第一个过滤器,然后调用它。

public class SizeFilter extends DocumentFilter {
private int maxCharacters;
private final DocumentFilter delegate;

public SizeFilter(int maxChars, DocumentFilter delegate) {
maxCharacters = maxChars;
this.delegate = delegate;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
delegate.insertString(fb, offs, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
throws BadLocationException {

if ((fb.getDocument().getLength() + str.length() - length) <= maxCharacters)
delegate.replace(fb, offs, length, str, a);
else
Toolkit.getDefaultToolkit().beep();
}
}
}

关于java - 如何使文本字段有 2 个文档过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12860072/

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