2017-06-19 16 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