String s=sb.getComponent(0).getText();
改成
String s=txt.getText();
ok!

解决方案 »

  1.   

    这我知道
    我这只是个测试
    在整个项目中没办法直接引用到txt,必须间接通过sb,所以我才问要怎么做啊????
      

  2.   

    String s=sb.getComponent(0).getText();是不是要在sb前面加个(TextArea)或者(TextComponent)进行一下转换.
    我觉得两个都可以.
      

  3.   

    void go(){
      String s=null;
      JTextArea t=getTextArea();
      if(t!=null){
         s=t.getText();
         System.out.println(s);
      }
    }
    JTextArea getTextArea(){
      Component c=null;
      for(int i=0;i<sb.getComponentCount();i++){
        c=sb.getComponent(i);
        if(c instanceof JViewport)
           return (JTextArea) ((JViewport) c).getView();
      }
      return null;
    }
      

  4.   

    import java.awt.*;
    import javax.swing.*;
    class Test1 extends JFrame
    {
    JTextArea txt=new JTextArea("haha");
    JScrollPane sb=new JScrollPane(txt);
             public Test1()
             {
                 sb.add(txt);
                 this.getcontentPane().add(sb);
                  
              }
    void go()
    {
    String s=txt.getText();
    System.out.println(s);
    }
    }    
    class Test
    {

    public static void main(String args[])
    {
    Test1 t=new Test1();
    t.go();
    }
    }
      

  5.   

    编写一个类继承JScrollPane,这样就可以存储/读取包含的对象了
      

  6.   

    发邮件把。[email protected]. 俺的QQ已经在俺觉得自己不再美丽妖艳或者英俊帅气后,就不再用了。