I am using JFrame and have a string containing the variable name. I want to get the value of that variable.
我正在使用JFrame并有一个包含变量名称的字符串。我想获得该变量的值。
String temp = "dim";
double temp_value = Double.parseDouble(temp.getText());
where dim
is name of variable in JFrame using swing library. Generally we use 2nd line of the code to get the value of the variable. How can we do in this case??
其中dim是使用swing库在JFrame中变量的名称。通常我们使用代码的第二行来获取变量的值。在这种情况下我们该怎么做?
1 个解决方案
#1
2
You have to use reflection API.
您必须使用反射API。
JFrame frame; // Instance of your JFrame ...
...
String temp = "dim";
Object value = frame.getClass().getField(temp).get(frame);
#1
2
You have to use reflection API.
您必须使用反射API。
JFrame frame; // Instance of your JFrame ...
...
String temp = "dim";
Object value = frame.getClass().getField(temp).get(frame);