程序中jscrollpane一开始就已经有了,现在想随时把后来新生成的jtree显示在已经有的这个jscrollpane上,怎么办呢?我用了jscrollpane上的add方法,好像没用啊?

解决方案 »

  1.   

    package java试验1;import javax.swing.*;
    import java.awt.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2005</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class Frame1 extends JFrame {
      BorderLayout borderLayout1 = new BorderLayout();
      JScrollPane jScrollPane1 = new JScrollPane();
      JTree jTree1 = new JTree();  public Frame1() {
        try {
          jbInit();
        }
        catch(Exception ex) {
          ex.printStackTrace();
        }
      }  void jbInit() throws Exception {
        this.setBounds(200,200,400,400);
        this.getContentPane().setLayout(borderLayout1);
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        jScrollPane1.getViewport().add(jTree1, null);//注意:******
      }  public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
        frame1.show();
      }
    }
      

  2.   

    去掉就用
    jScrollPane1.getViewport().removeAll();
    太好了,谢谢