import java.awt.*;
import java.awt.event.*;
public class TestPane {
Frame f = new Frame("TestScrollPane");
ScrollPane sp = new ScrollPane();
TextArea ta = new TextArea("",10,50,TextArea.SCROLLBARS_NONE);
sp.add(ta);
f.add(sp);
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
public static void main(String[] args) {
new TestPane();
}
}
编译出错,小弟请教大哥谁知道怎么回事~~
D:\javawork\Lesson9\TestPane.java:7: <identifier> expected
        sp.add(ta);
              ^
D:\javawork\Lesson9\TestPane.java:8: <identifier> expected
        f.add(sp);
             ^
D:\javawork\Lesson9\TestPane.java:9: <identifier> expected
        f.setSize(200,200);
                 ^
D:\javawork\Lesson9\TestPane.java:10: <identifier> expected
        f.setVisible(true);
                    ^
D:\javawork\Lesson9\TestPane.java:11: <identifier> expected
        f.addWindowListener(new WindowAdapter()
                           ^
5 errors

解决方案 »

  1.   

    sp.add(ta);
    f.add(sp);
    f.setSize(200,200);
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    类似的代码不能写在方法外,方法外只能定义和初始化类的属性资源!把这段代码写在构造函数里:
    import java.awt.*;
    import java.awt.event.*;
    public class TestPane {
      Frame f = new Frame("TestScrollPane");
      ScrollPane sp = new ScrollPane();
      TextArea ta = new TextArea("",10,50,TextArea.SCROLLBARS_NONE);
      public TestPane(){
        sp.add(ta);
        f.add(sp);
        f.setSize(200,200);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter()
          {
            public void windowClosing(WindowEvent e)
            {
              System.exit(0);
            }
          }
        );
      }
      public static void main(String[] args) {
      new TestPane();
      }
    }
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    public class TestPane {
       public TestPane(){//就是少了构造函数,LZ太粗心了
    Frame f = new Frame("TestScrollPane");
    ScrollPane sp = new ScrollPane();
    TextArea ta = new TextArea("",10,50,TextArea.SCROLLBARS_NONE);
    sp.add(ta);
    f.add(sp);
    f.setSize(200,200);
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );}
    public static void main(String[] args) {
    new TestPane();
    }
    }