NumberFormat文本字段,不带逗号 [英] NumberFormat text field without commas

查看:637
本文介绍了NumberFormat文本字段,不带逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JFormattedTextField 想接受5位数的数字。以下代码正常工作:

I have a JFormattedTextField which I want to accept numbers in the 5 digit range. The following code works correctly:

myNumberBox = new JFormattedTextField(NumberFormat.getIntegerInstance());

但是,当我在字段中键入12345并切换焦点时,会插入一个逗号,因为我的语言环境,使文本12,345。如何防止将逗号添加到我的输入中?更好的是,即使用户插入逗号,它们也可以被删除吗?

However, when I type "12345" into the field and switch focus, a comma is inserted due to my locale, making the text "12,345". How can I prevent commas from being added to my input? Better yet, can they be stripped out even if the user does insert commas?

推荐答案

你必须在NumberFormat中禁用分组像这样的对象:

You have to disable the grouping in your NumberFormat object like this:

NumberFormat format = NumberFormat.getIntegerInstance();
format.setGroupingUsed(false);
myNumberBox = new JFormattedTextField(format);

参见: NumberFormat.setGroupingUsed

这篇关于NumberFormat文本字段,不带逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆