2011-03-31 64 views
3

我使用Swing編寫應用程序。我的應用程序有一個文本框,我想要做的是這樣的:當用戶在該文本字段中輸入一個數字時,如果數字的數量是幾千,那麼它會在數字中自動附加一個逗號。 例如,如果用戶輸入1000,則應該自動更改爲1,000或1,000,000左右。關於JTextField和帶數字的文本格式的問題?

有人給我一些想法嗎?

+0

做事件偵聽器的東西嗎?你覺得哪個部分很難? – 2011-03-31 10:02:18

回答

3

您可以使用DecimalFormatNumberFormat類。 看到一個例子在這裏; http://www.exampledepot.com/egs/java.text/FormatNum.html

更新:

例子:

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

JTextField numberTextField = new JTextField(); 

numberTextField.setText(formattedNumber); 
+0

@Mudassir:@SamG:我們可以用這種方式在textField中顯示數字嗎?它只是格式化數字。不是嗎? – 2011-03-31 10:16:07

+0

@Harry:讓我檢查它... – Mudassir 2011-03-31 10:17:42

+0

@mudassir,格式化文本字段是在GUI中處理此問題的更合適的答案。 – jzd 2011-03-31 11:08:01