关于JTextField和带有数字的文本格式的问题? [英] Question about JTextField and text format with numbers?

查看:149
本文介绍了关于JTextField和带有数字的文本格式的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Swing编写应用程序。我的应用程序有一个文本框,我想要做的是这样的:当用户在该文本字段中输入一个数字,如果数字是在数千,那么它会自动附加一个逗号的数字。
例如,如果用户输入1000,那么它应该自动更改为1,000,或1,000,000左右。



有人给我一些想法吗?

解决方案

您可以使用 DecimalFormat NumberFormat 类。
在这里看一个例子; http://www.exampledepot.com/egs/java.text/FormatNum.html



更新:

b
$ b

  NumberFormat numberFormatter = new DecimalFormat(#,###,###); 
String formattedNumber = numberFormatter.format(10000000);

JTextField numberTextField = new JTextField();

numberTextField.setText(formattedNumber);


I writing an application using Swing. My application has a text box and what I want to do is this: when user enters a number in that text field, if the number is in the thousands, then it is automatically appended a comma in the number. For example, if user enters 1000, then it should be automatically changed to 1,000, or 1,000,000 or so.

Does anyone give me some ideas, please?

解决方案

You can use the DecimalFormat and NumberFormat classes. See an example here; http://www.exampledepot.com/egs/java.text/FormatNum.html

Update:

Example:

NumberFormat numberFormatter = new DecimalFormat("#,###,###");
String formattedNumber = numberFormatter.format(10000000);

JTextField numberTextField = new JTextField();

numberTextField.setText(formattedNumber);

这篇关于关于JTextField和带有数字的文本格式的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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