Java_左树右表

Java编程练习

要求

点击左边的树型结构上的节点选择班,在右边的表格中显示该班学生的相关信息

参考

代码

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;

public class test3_4 {
  public static void main(String[] args) {
    // 主方法创建该视窗
    new TreeWin("");
  }
}

class TreeWin extends JFrame implements TreeSelectionListener {

  private static final long serialVersionUID = 1L;

  JScrollPane rightPane;//用来显示表格的容器
  JTree tree;// 树
  Object name[];// 列名
  Object valueObjects[][];// 列数据
  JTable xJ1, xJ2, yS1, yS2, exampTable;// 表格

  void init() {
    // 窗口的初始化
    initTable();
    initRPanel();
    initTree();
    getContentPane().add(tree, BorderLayout.WEST);
    getContentPane().add(rightPane, BorderLayout.EAST);
    pack();
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    validate();
  }

  void initTree() {
    // 初始化树
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("专业");
    DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("信息与计算科学");
    DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("应用数学");
    DefaultMutableTreeNode node1sNode1 = new DefaultMutableTreeNode(new SClass("信计一班", xJ1));
    DefaultMutableTreeNode node1sNode2 = new DefaultMutableTreeNode(new SClass("信计二班", xJ2));
    DefaultMutableTreeNode node2sNode1 = new DefaultMutableTreeNode(new SClass("应数一班", yS1));
    DefaultMutableTreeNode node2sNode2 = new DefaultMutableTreeNode(new SClass("应数二班", yS2));
    root.add(node1);
    root.add(node2);
    node1.add(node1sNode1);
    node1.add(node1sNode2);
    node2.add(node2sNode1);
    node2.add(node2sNode2);
    tree = new JTree(root);
    tree.addTreeSelectionListener(this);// 给树加上监视器
  }

  void initRPanel() {
    exampTable.getColumnModel().getColumn(0).setPreferredWidth(100);
    rightPane = new JScrollPane(exampTable);
  }

  void initTable() {
    // 定义每个班的表格
    name = new Object[] { "学号", "姓名", "性别", "出生日期", "班级" };
    valueObjects = new Object[0][5];

    exampTable = new JTable(valueObjects, name);// 默认表格(空)
    valueObjects = new Object[][] { { "201807020101", "功夫熊猫", "男", "20060401", "2018070201" },
        { "201807020102", "鹤仙人", "男", "20010702", "2018070201" } };
    xJ1 = new JTable(valueObjects, name);
    valueObjects = new Object[][] { { "201807020201", "李小龙", "男", "19790323", "2018070202" },
        { "201807020202", "黄飞鸿", "男", "19560212", "2018070202" } };
    xJ2 = new JTable(valueObjects, name);
    valueObjects = new Object[][] { { "201807030101", "阿衰", "男", "20000925", "2018070301" },
        { "201807030102", "大脸妹", "女", "20011031", "2018070301" } };
    yS1 = new JTable(valueObjects, name);
    valueObjects = new Object[][] { { "201807030201", "唐三丈", "男", "18200906", "2018070302" },
        { "201807030202", "老鼠精", "女", "10000521", "2018070302" } };
    yS2 = new JTable(valueObjects, name);
  }

  class SClass {
    // 内部类,作为叶子节点使用
    String name;
    JTable fClass;

    SClass(String name, JTable tag) {
      this.name = name;
      this.fClass = tag;
    }

    public String toString() {
      return name;
    }
  }

  TreeWin(String name) {
    // 构造方法,定义视窗标题和其他空间
    super(name);
    init();
  }

  @Override
  public void valueChanged(TreeSelectionEvent e) {
    // 重写树的监听方法
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    if (node.isLeaf()) {
      SClass tag = (SClass) node.getUserObject();
      tag.fClass.getColumnModel().getColumn(0).setPreferredWidth(100);// 设置一下第一列的宽度
      rightPane.getViewport().add(tag.fClass);
      // 使用JSrollPane的getViewport方法获取里面的组件,由于JScrollPane只能放一个组件,直接add()方法可覆盖原先的内容
      // pack();
    } else {
      rightPane.getViewport().add(exampTable);
    }
  }
}

Leave a Comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注