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

左边是JTree,右边是JTable,用于显示表格(头痛的问题,求解...)
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class TestTree extends JFrame implements TreeSelectionListener{
  JPanel panel = new JPanel();
   
  JScrollPane scrollpane1,scrollpane2,scrollpane3; 
  JTable jt1,jt2; 
  JTree jtree;
  JSplitPane js;
  Object colName[]={"学号","姓名","年龄","家庭住址"};
  Object rowData[][]={{"2081714101","张三","20","湖北"},{"2081714102","王五","21","湖南"}};
  Object rowData1[][]={{"2091714101","李四","20","河南"},{"2091714102","王五","21","湖南"}};
 
  DefaultMutableTreeNode root = new DefaultMutableTreeNode("信息工程系"),
  jike = new DefaultMutableTreeNode("计科"),
  guangxin= new DefaultMutableTreeNode("光信"),
  jike1 = new DefaultMutableTreeNode("20817141"),
  jike2 = new DefaultMutableTreeNode("20917141");
   
  public TestTree() {
  setBounds(200,200,800,400);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  jike.add(jike1);
  jike.add(jike2);
  root.add(jike);
  root.add(guangxin);

  jtree = new JTree(root);
  jtree.addTreeSelectionListener(this);
  jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  scrollpane1 = new JScrollPane(jtree);
  add(scrollpane1);
   
   
  jt1 =new JTable(rowData,colName);
  scrollpane2 = new JScrollPane(jt1);
  // panel.add(scrollpane2); 
  // add(panel);
   
  jt2 =new JTable(rowData1,colName);
  scrollpane3 = new JScrollPane(jt2);
// panel.add(scrollpane3); 
  // add(panel);
   
   
  js = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane1,panel);
  js.setDividerLocation(150);
  js.setOneTouchExpandable(true);
  add(js);
  setVisible(true);
  setContentPane(js);
  }
  public void valueChanged(TreeSelectionEvent e){
  JTree tree = (JTree) e.getSource();
  DefaultMutableTreeNode selectionNode =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
  if (selectionNode==jike1){
  System.out.println("hello....");
  panel.add(scrollpane2); 
  add(panel);
  }  
  else if (selectionNode==jike2){
System.out.println("hello world....");
panel.add(scrollpane3); 
  add(panel);
  }


  }

  public static void main(String[] args){
  new TestTree();
  }
   
}

出现异常,而且显示的有问题,中间的分割线跑到最右边....高手指教啊

hello....
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: null
  at javax.swing.plaf.basic.BasicSplitPaneUI$BasicHorizontalLayoutManager.addLayoutComponent(BasicSplitPaneUI.java:1417)
  at javax.swing.plaf.basic.BasicSplitPaneUI$BasicHorizontalLayoutManager.addLayoutComponent(BasicSplitPaneUI.java:1518)