日期:2014-05-20  浏览次数:20837 次

菜鸟提问java跨方法调用数据
public class A extends Jpanel{
  public void a(){
   
  public static void main(String[] a){
  }
}

class B extends Jpanel{

}
如何在A的事件监听器中使用B的数据?

------解决方案--------------------
Java code


public class A extends JPanel{

    private B b = new B();
  public void a(){
      System.out.println(b.getData());
  }
  public static void main(String[] args){
  
  
  }
  
}

class B extends JPanel{

    private Object o = new Object();
    
    public Object getData()
    {
        return o;
    }
    
}

------解决方案--------------------
Java code
import javax.swing.*;

@SuppressWarnings("serial")
public class A extends JPanel {
    private B b;
    
    public A(){
        b = new B();
    }
    public void a() {
        b.gg();
    }

    public static void main(String[] a) {
        A aa = new A();
        aa.a();
    }
}

@SuppressWarnings("serial")
class B extends JPanel {
    public void gg(){
        System.out.println("abcdefg");
    }
}