比如说 JFrame 窗口中有 
JButton jb = new JButton("okbtn");
现在知道button 的名字和变量明,怎么获得一个reference来操作,
或者说,怎么获得实例jb?

解决方案 »

  1.   

    Class c = Class.forName("classname");
    Object obj = null;
    obj = c.newInstance(); //不帶引數Object[] arg = new Object[] {3.14159, 125}; //引數
    obj = ctor.newInstance(arg);//帶引數
      

  2.   

    可以用以下代码实现 
    Component[] com = getContentPane().getComponents();
            for (int i = 0; i < com.length; i++) {
                Component c = com[i];
                if(c instanceof JButton){
                    JButton b = (JButton)c;
                    if(b.getText()=="okbtn"){
                        System.out.println("b is the reference");
                    }
                }
            }