我正在尝试将Jtextfield的类型更改为double值,如下所示:
rangespaceField3.setText(bmi.getBMI());
getBMI方法返回一个double值,但会收到以下错误:
JTextComponent不适用于参数(double)
答案 0 :(得分:3)
答案 1 :(得分:2)
尝试rangespaceField3.setText(Double.toString(bmi.getBMI()));
答案 2 :(得分:2)
我已经基于JFormattedTextField实现了数字字段。
JRealNumberField和JLocalizedRealNumberField是BigDecimal的文本字段。
它们还支持最小值和最大值。
也许您觉得它们很有用(图书馆是开源的):
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JRealNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JDoubleField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFloatField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedDoubleField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedFloatField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JWholeNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JByteField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JIntegerField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLongField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JShortField.html
教程:
http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/number/index.html
主页:
下载:
http://sourceforge.net/projects/softsmithy/files/softsmithy/
的Maven:
<dependency>
<groupid>org.softsmithy.lib</groupid>
<artifactid>lib-core</artifactid>
<version>0.1</version>
</dependency>
答案 3 :(得分:1)
如果您只想将double的字符串表示形式放入文本字段中,那么可以在String类中使用valueOf()方法。
rangespaceField3.setText(String.valueOf(bmi.getBMI()));
答案 4 :(得分:0)
试试这个......
//使用空字符串converinate double值,将double转换为string
rangespaceField3.setText(bmi.getBMI()+ “”);
OR
//在方法返回的double值上调用toString(),它也将转换为String
(bmi.getBMI()的toString())rangespaceField3.setText;