为什么 JFormattedTextField 是邪恶的?
在这个问题有什么方法可以只接受 JTextField 中的数字值? 答案之一表明 JFormattedTextField 有问题。
我还没有使用过它,但是有人可以扩展(或不同意)此类的问题吗?
In this question Is there any way to accept only numeric values in a JTextField? one of the answers suggested that JFormattedTextField had issues.
I've not yet used it, but could somebody please expand (or disagree) on the issues with this class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JFormattedTextField
是邪恶的,因为可用性很差。例如,如果文本字段只能接受整数值,则 JFormattedTextField 将允许任何旧的垃圾,然后在焦点丢失时抱怨。更糟糕的是,当焦点丢失时,它有一系列可配置的行为(一个明显的迹象表明它们都不是正确的选择)。它还具有“k3wl”格式化程序选择。它与文档的交互似乎也很差(它尝试安装
DocumentFilter
,但这不是Document
界面的一部分)。JFormattedTextField
is evil because of poor usability. For instance, if a text field should only be able to accept an integer value,JFormattedTextField
will allow any old rubbish and then complain when focus is lost. To make matters worse, it has a range of configurable behaviours when focus is lost (a clear sign that none of them is the correct choice).It also has "k3wl" formatter selection. It also seems to have poor interaction with the document (it tries to install a
DocumentFilter
, but that is not part of theDocument
interface).我承认 JFormattedTextField 不是最容易使用的类,是的,它确实有问题,但像大多数组件一样,它在某些方面做得不错,但在其他方面做得很糟糕。
事实上,它确实允许您在使用 MaskFormatter 时输入数据时编辑整数值。因此对于简单的任务来说它是有效的。
数据编辑是一项复杂的任务。有时您需要在输入数据时对其进行编辑(在检查整数值的情况下)。有时您需要在输入数据后对其进行编辑(在验证日期的情况下)。当然,您需要决定如何处理无效数据。
在执行重要任务时可能需要配置一定量的参数。它们可能并不总是按照您希望的方式或您期望的方式工作,因此您确实需要进行试验以找出何时可以/应该使用此组件。
I'll admit JFormattedTextField is not the easiest class to use, and yes it does have issues but like most components it does a reasonable job at some things and a bad job at others.
It does in fact allow you to edit for Integer values as the data is typed when you use a MaskFormatter. So for simple tasks it can be effective.
Editing of data is a complex task. Sometimes you need to edit the data as it is entered (in the case of checking for Integer values). Sometimes you need to edit the data after it is entered (in the case of validating a date). Then of course you need to decide how to handle invalid data.
There can be a certain amount of configuring the parameters when doing non-trivial tasks. They may not always work the way you want them to or the way you expect them to so you do need to experiment to find out when you can/should use this component.
我目前正在与 JFormattetTextField 作斗争。我正在尝试使用
NumberFormat.getPercentInstance()
来格式化我的百分比值。最糟糕的是
.getValue()
返回的值与用户看到的值不同。例如,如果我在格式化文本字段中输入25,5%
,则会将其四舍五入为26%
,但.getValue()
返回 <代码>0.255。那就是邪恶。I'm struggling with JFormattetTextField for the moment. I'm trying to use
NumberFormat.getPercentInstance()
for formatting my percent-value.The worst thing is that
.getValue()
doesn't return the same value as the user is seeing. In example if I type25,5%
in the formatted textfield, then it rounds it to26%
, but.getValue()
returns0.255
. That is Evil.