关于JTextField和带有数字的文本格式的问题? [英] Question about JTextField and text format with numbers?
问题描述
例如,如果用户输入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屋!