先吧你所有的jTextField_zdcd1 ...放入Object []list = new Object[n](也可以直接放到Vector对象中),然后用((JTextField)list[i]).gettext();
这其实就是简单的泛型技术的应用。

解决方案 »

  1.   

    非常谢谢,,有道理。请高手更加清楚说明,谢谢!!我有jTextField_zdcd1 ,jTextField_zdcd2,jTextField_zdcd3。
      有jTextField_name1, jTextField_name2,jTextField_name3........
      有jTextField_sex1,jTextField_sex2...........等等大概有160个控件,但是都有规律(我在命名的时候已经作死了)。。
    如何动态取出这些160个控件的内容。谢谢啊!!!!非常谢谢,作一个例子何如。谢谢!!!!!
    我是在jb中写的,不是jsp.
      

  2.   

    首先,用
    int comCount=this.getComponentCount();
    获取所有控件的数目,然后用
    Component[] allCom=this.getComponents()
    取得所有控件的集合,然后就可以通过
    allCom[0]来取得第一个控件,相应的allCom[1]代表第二个控件
    ……………………………………
    allCom[comCount-1]第comCount个控件
    用循环就可以取得所有控件了,如果想对某一个控件进行操作的话,可以通过getName()取得name来操作
    for(int i=0;i<allCom.length;i++) {
      if(allCom[i].getName().equals("componentName"))
        System.out.println(allCom[i].getName());
    }
      

  3.   

    这个方法非常不错谢谢
    可是有一个问题,我问??因为我主要的目的是想取这里面的数??比如:
    jTextField_1.getText()
    choice_szlx.getSelectedItem()
    checkbox_edit.getState()就是说不同的控件我都想取出其中的数值!!!!!!!!如何为好啊
      

  4.   

    for(int i=0;i<allCom.length;i++) {
      if(allCom[i] instanceof JTextField)
        ((JTextField)allCom[i]).getText();//取值
      else if(allCom[i] instanceof JComboBox)
        ((JSelectedItem)allCom[i]).getSelectedItem();//取值
      else if(allCom[i] instanceof JCheckBox)
        ((JCheckBox)allCom[i]).getState();//取值
    }
      

  5.   

    还有一个方法,你自己重写控件,
    interface MyComp{
       Object getValue();
    }class MyTextField extends JTextField implements MyComp{
       .....
       public Object getValue(){
          return getText();
       }
    }class MyChoice extends JComboBox implements MyComp.....Component[] comps=getComponents();
    for(......){
       MyComp c=(MyComp)comps[i];
       c.getValue();
    }