JFormattedTextField 格式化百分比数字?
我想使用 JFormattedTextField 将浮点数格式化为百分比值,允许输入 0 到 100%(转换为 0.0f-1.0f),始终显示百分号并不允许任何无效字符。
现在我已经尝试了一些 NumberFormat.getPercentInstance() 和 NumberFormatter 属性,但没有成功。
有没有办法使用标准类创建遵守这些规则的 JFormattedTextField?或者我必须实现自己的 NumberFormatter 吗?
这就是我到目前为止所拥有的(无法输入 100%,输入 0 会完全破坏它):
public class MaskFormatterTest {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
NumberFormat format = NumberFormat.getPercentInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMaximum(1.0f);
formatter.setMinimum(0.0f);
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
JFormattedTextField tf = new JFormattedTextField(formatter);
tf.setColumns(20);
tf.setValue(0.56f);
frame.add(tf);
frame.pack();
frame.setVisible(true);
}
}
I would like to format a float number as a percent-value with JFormattedTextField that allows inputs from 0 to 100 percent (converted to 0.0f-1.0f), always shows the percent sign and disallows any invalid characters.
Now I have experimented a bit with NumberFormat.getPercentInstance() and the NumberFormatter attributes but without success.
Is there a way to create a JFormattedTextField that obeys to these rules with the standard classes? Or do I have to implement my own NumberFormatter?
That's what I have so far (no way to input 100%, entering a 0 breaks it completly):
public class MaskFormatterTest {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
NumberFormat format = NumberFormat.getPercentInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMaximum(1.0f);
formatter.setMinimum(0.0f);
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
JFormattedTextField tf = new JFormattedTextField(formatter);
tf.setColumns(20);
tf.setValue(0.56f);
frame.add(tf);
frame.pack();
frame.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我成功了。该解决方案远非简单,但至少它完全符合我的要求。除了返回双精度数而不是浮点数。一个主要限制是它不允许小数位,但现在我可以忍受这一点。
Ok, I've made it. The solution is far from simple, but at least it does exactly what I want. Except for returning doubles instead of floats. One major limitation is that it does not allow fraction digits, but for now I can live with that.
1)考虑使用 JSpinner 而不是
JFormattedTextField< /code> 因为你可以设置
noreferrer">SpinnerNumberModel 用于来自 API 的
,并且通过简单的
JSpinner
破解(使用SpinnerNumberModel
),它不允许其他输入作为数字,否则存在可能输入任何Chars
2) 对于
JFormattedTextField
你必须实现并且对于
JFormattedTextField
你必须编写这两种情况如果值小于或大于所需范围,则 catch 的解决方法...编辑:
。
.
根本不正确,:-) 你离......简单错误:-) 还很远,你的结果有小错误,请看一下这段代码
1) consider using JSpinner instead of
JFormattedTextField
because there you can set SpinnerNumberModel for initial valuesfrom API
and with simple hack for
JSpinner
(withSpinnerNumberModel
) it doesn't allows another input as Digits, otherwise is there possible input any ofChars
2) for
JFormattedTextField
you have to implementsand of both cases for
JFormattedTextField
you have to write workaround for catch if value is less or more than required range ...EDIT:
.
.
not true at all, :-) you are so far from ... simple wrong :-), there is small mistake with your result, please look at this code
恕我直言 https://docs.oracle.com/javase/tutorial/uiswing/ Components/formattedtextfield.html 给出了一个很好的示例(请参阅“指定格式化程序和使用格式化程序工厂”部分)。
关键是使用百分比格式来显示值并使用自定义 NumberFormatter 来编辑值。这种方法还允许使用小数位。
Imho https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html gives a pretty good example (see section "Specifying Formatters and Using Formatter Factories").
The key is to use a Percent Format to display values and a custom NumberFormatter to edit values. This approach also allows the use of fraction digits.