有关NumberFormatter的问题

有关NumberFormatter的问题

本文介绍了有关NumberFormatter的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码,我希望这会增加以下约束:对于文本字段,只有整数输入才有效:

Here is the code, I am hoping that this will add the constraint that only integer input is valid for the text field:

JFormattedTextField ftf =  new JFormattedTextField ();

NumberFormat format = NumberFormat.getNumberInstance();
format.setParseIntegerOnly(true);
ftf.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(format), new NumberFormatter(format), new NumberFormatter(format)));

但是令我惊讶的是,当我输入100aaa时,它是有效的100.111,它也是有效的.如何限制仅整数是有效输入?

But what suprised me is that when I input 100aaa, it is valid, 100.111, it is valid too.how can I make it to restrict that only integer is valid input?

推荐答案

您需要setAllowsInvalid(false)在NumberFormatter对象上.

You need to setAllowsInvalid(false) on the NumberFormatter objects.

这篇关于有关NumberFormatter的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:55