[英]Can we get String value as a variable reference? (Variable Name)
我有一個這樣的字符串數組,
String[] components = {"jButton1,jLabel1,jTextField1"};
我想使用這樣的可變字符串名稱,
components[0].setText("Some Text");
我可以使用字符串值作為變量名嗎? 這所有變量聲明都在我的代碼中。
雖然在使用i18n時不需要更改變量名,但是為了獲得變量名,您可以嘗試以下操作:
Class<?> myClass = this.getClass();
Field field = myClass.getField("fieldName");
if(field instanceof JButton)
((JButton)field).setText("My text");
看一下本教程,可能會有所幫助http://omtlab.com/java-reflection-tutorial/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.