使用字符串varriable中的值访问变量

时间:2023-01-05 21:37:38
String a="jTextField1";

instead of writing jTextField1.setText("foo") how can i use the value saved in 'a' to setText() in that text field? Is it possible??

而不是写jTextField1.setText(“foo”)如何在该文本字段中使用'a'中保存的值来设置setText()?可能吗??

Thanks in advance.

提前致谢。

1 个解决方案

#1


4  

Assign a name to your text fields and put them in a Map.

为文本字段指定名称并将其放在Map中。

Map<String, JTextField> textFieldsByName = new HashMap<>();
textFieldsByName.put("jTextField1", someTextField);
...
String textFieldName = ...;
textFieldsByName.get(textFieldName).setText("foo");

#1


4  

Assign a name to your text fields and put them in a Map.

为文本字段指定名称并将其放在Map中。

Map<String, JTextField> textFieldsByName = new HashMap<>();
textFieldsByName.put("jTextField1", someTextField);
...
String textFieldName = ...;
textFieldsByName.get(textFieldName).setText("foo");