tbf.getobj();  -------------> tbf.getObj();
tbf.getSize(300,300); -------->tbf.setSize(300,300);
估计这两处是笔误,改过来就能编译了,不过你这个程序好象很有问题。

解决方案 »

  1.   

    我太粗心了,谢谢 dreamchen
      

  2.   

    getSize() 也是存在的~~只是 返回对象格式的大小
      

  3.   

    我这样改完就好了
    只是不知道这样写是不是很笨,有没有更好的办法
    我是初学,请各位多指教import java.awt.*;
    import java.awt.event.*;
    class TestButtonFrame extends Frame
    {
    Button b1 =new Button("blue");
    Button b2 =new Button("red");
    Button b3 =new Button("yellow");
    public TestButtonFrame()
    {
    this.add(b1); //不加this 也是一样的,可不可以理解为自动调用当前对象?
    this.add(b2);
    this.add(b3);
    MyListener ml1=new MyListener(Color.blue,getObj());
    MyListener ml2=new MyListener(Color.red,getObj());
    MyListener ml3=new MyListener(Color.yellow,getObj());
    setLayout(new GridLayout());
    b1.addActionListener(ml1);
    b2.addActionListener(ml2);
    b3.addActionListener(ml3);

    }
    public TestButtonFrame getObj()
    {
    return(this);
    }
    public static void main(String args[])
    {
    TestButtonFrame tbf=new TestButtonFrame();
    tbf.getObj();
    tbf.setSize(300,300);
    tbf.show();

    }
    }
    class MyListener implements ActionListener
    {
    private Color bgcolor;
    private TestButtonFrame obj;
    public MyListener(Color c,TestButtonFrame o)
    {
    bgcolor=c;
    obj=o;
    }
    public void actionPerformed(ActionEvent e)
    {
    obj.setBackground(bgcolor);
    obj.repaint();
    }

    }