我为 JFormattedTextField 制作的 NumberFormatter 函数不允许输入十进制数字
The NumberFormatter function I made for JFormattedTextField, doesn't allow typing decimal numbers
为了将用户的输入限制在特定的数字范围内,我创建了这个函数:
public static final Locale GR=new Locale("el", "GR");
public static NumberFormatter getMyNumberFormatter(){
NumberFormatter formatter = new NumberFormatter(NumberFormat.getInstance(GR));
formatter.setValueClass(Double.class);
formatter.setAllowsInvalid(false);
formatter.setCommitsOnValidEdit(true);
formatter.setMinimum(0.0);
formatter.setMaximum(10000000.0);
DecimalFormat df = (DecimalFormat)DecimalFormat.getInstance(GR);
df.setGroupingUsed(true);
formatter.setFormat(df);
return formatter;
}
我将此格式化程序应用于 JFormatedTextfield,但它仅 对整数值有效。我希望用户能够键入从 0.0 到 10000000.0 的浮点数值,但当前的格式化程序只允许整数。自动分组工作完美。有什么建议吗?
我记得 JFormattedTextField
使用起来很痛苦。
我假设 DecimalFormat 不允许只有小数点的数字,基于 javadoc 的以下部分:
If you are going to allow the user to enter decimal values, you should either force the DecimalFormat to contain at least one decimal (#.0###), or allow the value to be invalid setAllowsInvalid(true)
. Otherwise users may not be able to input decimal values.
您也可以尝试添加
df.setDecimalSeparatorAlwaysShown(true);
请注意:JFTF 是使用一种 NumberFormat 创建的,后来又设置了一个新格式,这有点令人困惑。更直接(未测试):
DecimalFormat df = (DecimalFormat)DecimalFormat.getInstance(GR);
df.setGroupingUsed(true);
df.setDecimalSeparatorAlwaysShown(true);
NumberFormatter formatter = new NumberFormatter(df);
...
或者只是
NumberFormatter formatter = new NumberFormatter();
...
formatter.setFormat(df);
建议(IMO 比使用 JFTF 更好):
扩展 DocumentFilter
并将其设置为新 PlainDocument 的过滤器。使用使用该文档的 JTextField。
为了将用户的输入限制在特定的数字范围内,我创建了这个函数:
public static final Locale GR=new Locale("el", "GR");
public static NumberFormatter getMyNumberFormatter(){
NumberFormatter formatter = new NumberFormatter(NumberFormat.getInstance(GR));
formatter.setValueClass(Double.class);
formatter.setAllowsInvalid(false);
formatter.setCommitsOnValidEdit(true);
formatter.setMinimum(0.0);
formatter.setMaximum(10000000.0);
DecimalFormat df = (DecimalFormat)DecimalFormat.getInstance(GR);
df.setGroupingUsed(true);
formatter.setFormat(df);
return formatter;
}
我将此格式化程序应用于 JFormatedTextfield,但它仅 对整数值有效。我希望用户能够键入从 0.0 到 10000000.0 的浮点数值,但当前的格式化程序只允许整数。自动分组工作完美。有什么建议吗?
我记得 JFormattedTextField
使用起来很痛苦。
我假设 DecimalFormat 不允许只有小数点的数字,基于 javadoc 的以下部分:
If you are going to allow the user to enter decimal values, you should either force the DecimalFormat to contain at least one decimal (#.0###), or allow the value to be invalid
setAllowsInvalid(true)
. Otherwise users may not be able to input decimal values.
您也可以尝试添加
df.setDecimalSeparatorAlwaysShown(true);
请注意:JFTF 是使用一种 NumberFormat 创建的,后来又设置了一个新格式,这有点令人困惑。更直接(未测试):
DecimalFormat df = (DecimalFormat)DecimalFormat.getInstance(GR);
df.setGroupingUsed(true);
df.setDecimalSeparatorAlwaysShown(true);
NumberFormatter formatter = new NumberFormatter(df);
...
或者只是
NumberFormatter formatter = new NumberFormatter();
...
formatter.setFormat(df);
建议(IMO 比使用 JFTF 更好):
扩展 DocumentFilter
并将其设置为新 PlainDocument 的过滤器。使用使用该文档的 JTextField。