Java 使用带有双精度值的 setText(..) 方法设置文本字段。图形用户界面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20596848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:01:18  来源:igfitidea点击:

Setting the text field using setText(..) method with a double value. Java GUI

javaswinguser-interfacejtextfield

提问by user3102872

I'm trying to use setText(..)and sending a double value to it but it says

我正在尝试使用setText(..)并向其发送双倍值,但它说

setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (double)

回答by zbr

You need to convert your doubleto a Stringand then pass that Stringas an argument to the setText(String)method. This is how you convert a doubleto a String:

您需要将您的转换double为 a String,然后将其String作为参数传递给该setText(String)方法。这是将 a 转换double为 a 的方式String

double d = 3.14;
String s = String.valueOf(d);
// s is now "3.14"