可以说,用户必须在Jtextfield中输入Double值,然后才能计算出该值。
但是,如果用户突然使用多于1个周期,将触发一个NumberFormatException异常,所以我假定溶液将使用文档滤波器滤除任何额外的周期或捕捉异常并通知一个无效的输入的用户使用文档过滤器过滤多个句点(。)
Currenty使用某个DocumentFilter只允许数字和周期,但我的问题是如何筛选出的第二周期
PlainDocument filter = new PlainDocument();
filter.setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr)
throws BadLocationException
{
fb.insertString(off, str.replaceAll("[^0-9.]", ""), attr);
}
@Override
public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)
throws BadLocationException
{
fb.replace(off, len, str.replaceAll("[^0-9.]", ""), attr);
}
});
apm.setDocument(filter);
例
无效 INPUT:1.2.2
有效 INPUT:1.22
不知道在说什么,为我工作,为更好的帮助,尽快发布一个[SSCCE](http://sscce.org/),简短,可运行,可编译 – mKorbel
我会编码过滤器,不允许第二个时期。你有什么尝试?它怎么不工作?你好@mKorbel! –
我自己,我会等待sscce。 –