问题
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?
回答1:
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);
回答2:
Try formattedTextFields
Also one more example. May be helpful to you.
回答3:
Have a look at group seperators in DecimalFormat .
来源:https://stackoverflow.com/questions/5498109/question-about-jtextfield-and-text-format-with-numbers