2017-06-19 45 views

回答

1

下面是類似的代碼片段,您可以嘗試

新的更新(成功工作):

JTextField yourTextFieldName = new JTextField(); 
DecimalFormat dc = new DecimalFormat("0.00"); 
String formattedText = dc.format(Double.parseDouble(yourTextFieldName.getText())); 
System.out.println(formattedText); 

舊版本(非reccomented):

JTextField yourTextFieldName = new JTextField(); 
yourTextFieldName.setText(String.format("%.2f",Double.parseDouble(custom.getValue()))); 
+1

對於純格式化來說,'JFormattedTextField'將是首選 – MadProgrammer

+0

我已經添加了一個新的代碼片段,它我試過了,它工作正常。 –

+0

現在在字段中輸入 - 它不會保存格式,不會限制你可以輸入的內容,這更多的是我「認爲」OP正在嘗試請求的內容......但是誰知道,問題很容易解釋,因此無法回答 – MadProgrammer