本人想通过预先存储的java控件变量名(字符串形式),然后通过程序引用控件,对其进行赋值操作,举例:
javax.swing.JTextField jTextField1;
如何通过这个字符串"jTextField1",来引用它?
 求教有这方面经验的人,能够给贴出代码看看,谢谢

解决方案 »

  1.   

    用反射,或者是一种变通的Hashmap方法HashMap<String,JTextField> hash=new HashMap<String,JTextField>();hash.put("jTextField1",jTextField1);根据名称获取组件JTextField item=hash.get("jTextField1");
      

  2.   

    Class fieldClass = null;
                JTextField aTextField = null;
    String yourFieldName = "jTextField1";
                try {
                    fieldClass = Class.forName(yourFieldName);
                    aTextField = (JTextField)fieldClass.newInstance();
                }
                catch (IllegalAccessException ex1) {
                    aTextField = new JTextField();
                }
                catch (InstantiationException ex1) {
                    aTextField = new JTextField();
                }
                catch (ClassNotFoundException ex) {
                    aTextField = new JTextField();
                }