gpt4 book ai didi

java - 如何使用 DocumentFilter 设置可以在 Jformattedtextfield 中输入的最大值和最小值?

转载 作者:行者123 更新时间:2023-11-30 08:13:36 29 4
gpt4 key购买 nike

我是 Java 编程新手。我有一个文档过滤器,只允许数字值和带“.”的小数。在 JFormattedTextField 中。现在我想在该过滤器中实现一个方法,只允许最大值和最小值,就像我想要输入的任何数字值一样,它必须在 [0-1] 之间,否则它不应该接受输入的任何内容。

现在我不想使用JSpinner,因为从0到1有数百万个小数,例如:0.0001、0.0012 ...

这是我的documentFilter代码:

package javagui.views;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

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

public class SeulementNumerique extends PlainDocument {
public static final String FLOAT = "0123456789.";
protected String acceptedChars = null;
protected boolean negativeAccepted = false;
public SeulementNumerique() {
this(FLOAT);
}
public SeulementNumerique(String acceptedchars) {
acceptedChars = acceptedchars;
}

public void setNegativeAccepted(boolean negativeaccepted) {
if (acceptedChars.equals(FLOAT)) {
negativeAccepted = negativeaccepted;
acceptedChars += "-";
}
}

public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {

if (str == null)
return;
for (int i = 0; i < str.length(); i++) {
if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1)
return;
}
if (acceptedChars.equals(FLOAT) || (acceptedChars.equals(FLOAT + "-") && negativeAccepted)) {
if (str.indexOf(".") != -1) {
if (getText(0, getLength()).indexOf(".") != -1) {
return;
}
}
}
if (negativeAccepted && str.indexOf("-") != -1) {
if (str.indexOf("-") != 0 || offset != 0) {
return;
}
}
super.insertString(offset, str, attr);
}
}

现在在我使用的主代码中调用该过滤器:

formattedTextField_5 = new JFormattedTextField();
formattedTextField_5.setDocument(new SeulementNumerique());

是否有任何简单的方法来设置可以在 JFormattedTextField 中输入的最大值和最小值?

最佳答案

JTextField 可以与自定义 DocumentFilter 一起使用检查值是否为数字且在指定范围内。下面是一个示例 DocumentFilter,它可以完成此任务:

public class RestrictedNumberDocumentFilter extends DocumentFilter{

private double min;
private double max;

public RestrictedNumberDocumentFilter(double min, double max){
if ( max < min ){
double temp = max;
max = min;
min = temp;
}
this.min = min;
this.max = max;
}

@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr) throws BadLocationException {
StringBuilder sb = new StringBuilder(fb.getDocument().getText(0, fb.getDocument().getLength()));
if ( test(sb.toString()) ){
fb.insertString(off, str, attr);
}else{
//warn
}

}
@Override
public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)throws BadLocationException{
StringBuilder sb = new StringBuilder(fb.getDocument().getText(0, fb.getDocument().getLength()));
sb.replace(off, off+len, str);
if ( test(sb.toString()) ){
fb.replace(off, len, str, attr);
}else{
//warn
}

}
/**
* Sanitized the input
* @param val
* @return
*/
private boolean test(String val){
try{
double d = Double.parseDouble(val);
if ( d >= min && d <= max ){
return true;
}
return false;
}catch(NumberFormatException e){
return false;
}
}
}

要使用此类,请为 JTextField 的文档设置 DocumentFilter

JTextField field = new JTextField();
AbstractDocument doc = (AbstractDocument )field.getDocument();
doc.setDocumentFilter(new RestrictedNumberDocumentFilter (min, max));

您可能需要针对您预期的任何输入进一步自定义此类。

您可以使用 JFormattedTextField 执行类似的操作,但使用自定义 DocumentFilter 可能有其自身的问题,如 here 中所述。

关于java - 如何使用 DocumentFilter 设置可以在 Jformattedtextfield 中输入的最大值和最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991936/

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