Java 从文本框获取文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19119392/
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
Get text from textfield
提问by Ralph Lorenz
I'm making a GUI program. In my first program I have the following code:
我正在制作一个 GUI 程序。在我的第一个程序中,我有以下代码:
double num1;
num1 = Double.parseDouble(guess.getText());
I believe that this code gets the value from the text field and converts it to double.
我相信这段代码从文本字段中获取值并将其转换为双精度值。
How can I get the value and convert it to String or Char?
如何获取值并将其转换为 String 或 Char?
回答by dARKpRINCE
The getText()
already returns the text as String.
Btw, be careful of Exceptions due to parse error. But your on the right track. :)
该getText()
已返回文本为String。顺便说一句,注意由于解析错误引起的异常。但你在正确的轨道上。:)
回答by Ayvadia
The getText()
method returns a String. when you use .parseDouble
what you are really doing is turning the string the user entered and into a double therefore in the case of a string you do not have to use a .parse method because the value called is already a string. In the case of a character you would have to use something like this:
该getText()
方法返回一个字符串。当您使用.parseDouble
您真正在做的事情时,将用户输入的字符串转换为双精度值,因此在字符串的情况下,您不必使用 .parse 方法,因为调用的值已经是一个字符串。在字符的情况下,您必须使用以下内容:
String text = jTextField1.getText();
if (text.length() > 1 && !text.contains(" ") && !text.contains(",")) {
//make sure that its length is not over 1, and that it has no spaces and no commas
char ch = text;
} else {
//if a space or comma was found no matter how big the text it will execute the else..
System.out.println("this is not allowed");
jTextField1.setText("");
}
回答by afsantos
Since the getText()
already returns a String
, storing its value as a String
is trivial.
由于getText()
已经返回 a String
,将其值存储为 aString
是微不足道的。
In order to parse a double
, you've already done it, just watch for the NumberFormatException
, in case of invalid input.
为了解析 a double
,您已经完成了,只需注意NumberFormatException
, 以防输入无效。
To store its value as a char
, that depends on your requirements. Do you want the first character? Do you require the string to have only a single character? Is any character valid? And so on.
要将其值存储为 a char
,这取决于您的要求。你想要第一个字符吗?您是否要求字符串只有一个字符?任何字符都有效吗?等等。
// Storing the value as a String.
String value = guess.getText();
// Storing the value as a double.
double doubleValue;
try {
doubleValue = Double.parseDouble(value);
} catch (NumberFormatException e) {
// Invalid double String.
}
// Storing the value as a char.
char firstChar = value.length() > 0 ? value.charAt(0) : (char) 0;
// Require the String to have exactly one character.
if (value.length() != 1) {
// Error state.
}
char charValue = value.charAt(0);
回答by Vasu Gupta
The getText()
function already fetches a String
value from the Textfield
.
该getText()
函数已经String
从Textfield
.
For example:
例如:
String var = guess.getText();
Here, var
is storing the String
value received from the Textfield
.
这里,var
是存储String
从Textfield
.
回答by user8615675
use String.valueOf() instead of Double.parseDouble() this will help you convert double into string value
使用 String.valueOf() 而不是 Double.parseDouble() 这将帮助您将 double 转换为字符串值