如何将jtextfield中的输入严格限制为double值?

时间:2012-10-19 15:53:33

标签: java swing user-interface jformattedtextfield

我正在编写一个gui,我想使用JFormattedTextField来验证我的Input为double值,但是amountFormatter并没有给我输入正确的输入。所以我尝试用(“##########”)创建一个自己的格式化程序,它给了我一个10位数的数字,但是我无法验证双值(因为它们通常有一个'。') ...

所以我的问题是:如何使用Jformatted TextFields简单地验证double值,或者使用另一个Swing Component有更简单的方法?

更新 的 谢谢你的答案!但是,是否可能有JFormatted TextField的解决方案?

2 个答案:

答案 0 :(得分:2)

修改

你可以从

开始
NumberFormat format = NumberFormat.getNumberInstance();
format.setGroupingUsed(false);
format.setGroupingUsed(true);// or add the group chars to the filter
format.setMaximumIntegerDigits(10);
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(5);
format.setRoundingMode(RoundingMode.HALF_UP);

答案 1 :(得分:1)

我已经基于JFormattedTextField实现了数字字段。

它们还支持最小值和最大值。

也许您觉得它们很有用(图书馆是开源的):

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JDoubleField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedDoubleField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JWholeNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JByteField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JIntegerField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLongField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JShortField.html

教程:

http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/number/index.html

更多信息:

http://puces-blog.blogspot.ch/2012/07/news-from-software-smithy-version-02.html

主页:

http://www.softsmithy.org

下载:

http://sourceforge.net/projects/softsmithy/files/softsmithy/

的Maven:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.2</version>   
</dependency>