建立图书馆目录索引的树型目录,并显示书籍的相关信息

建立图书馆目录索引的树型目录,并显示书籍的相关信息

编写主类,创建目录窗口

public class Example9_21{
    public static void main(String args[]){
        TreeWin win = new TreeWin(); 
    } 
}

构造识别类:定义书名,文本内容

public class Goods{
    String name;
    String txt;
    Goods(String n,String t){
       name=n;
       txt = t;
    }
    public String toString() { //返回对象的串表示
       return name;
    }
}

在网格布局(1行2列)中添加各组件,文本由于审核原因,故用 * 代替

public class TreeWin extends JFrame implements TreeSelectionListener{
    JTree tree;
    JTextArea showText;
    TreeWin(){
    	DefaultMutableTreeNode txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9,txt10,txt11,txt12,txt13,txt14,txt15,txt16;
       DefaultMutableTreeNode root=new DefaultMutableTreeNode("图书馆");  
       DefaultMutableTreeNode class1=new DefaultMutableTreeNode("军事");  
       DefaultMutableTreeNode class2=new DefaultMutableTreeNode("艺术"); 
       DefaultMutableTreeNode class3=new DefaultMutableTreeNode("文学");
       DefaultMutableTreeNode class4=new DefaultMutableTreeNode("医药、卫生");
       DefaultMutableTreeNode nextclass1=new DefaultMutableTreeNode("军事理论");
       DefaultMutableTreeNode nextclass2=new DefaultMutableTreeNode("世界军事");
       DefaultMutableTreeNode nextclass3=new DefaultMutableTreeNode("雕塑");
       DefaultMutableTreeNode nextclass4=new DefaultMutableTreeNode("工艺美术");
       DefaultMutableTreeNode nextclass5=new DefaultMutableTreeNode("小说");
       DefaultMutableTreeNode nextclass6=new DefaultMutableTreeNode("文学");
       DefaultMutableTreeNode nextclass7=new DefaultMutableTreeNode("预防医学、卫生学");
       DefaultMutableTreeNode nextclass8=new DefaultMutableTreeNode("基础医学");
       
       txt1=new DefaultMutableTreeNode(new Goods("*"));           
       txt2=new DefaultMutableTreeNode(new Goods("*")); 
       txt3=new DefaultMutableTreeNode(new Goods("*")); 
       txt4=new DefaultMutableTreeNode(new Goods("*")); 
       txt5=new DefaultMutableTreeNode(new Goods("*")); 
       txt6=new DefaultMutableTreeNode(new Goods("*")); 
       txt7=new DefaultMutableTreeNode(new Goods("*")); 
       txt8=new DefaultMutableTreeNode(new Goods("*")); 
       txt9=new DefaultMutableTreeNode(new Goods("*")); 
       txt10=new DefaultMutableTreeNode(new Goods("*")); 
       txt11=new DefaultMutableTreeNode(new Goods("*")); 
       txt12=new DefaultMutableTreeNode(new Goods("*")); 
       txt13=new DefaultMutableTreeNode(new Goods("*")); 
       txt14=new DefaultMutableTreeNode(new Goods("*")); 
       txt15=new DefaultMutableTreeNode(new Goods("*")); 
       txt16=new DefaultMutableTreeNode(new Goods("*")); 
       root.add(class1);
       root.add(class2);
       root.add(class3);
       root.add(class4);
       class1.add(nextclass1);
       class1.add(nextclass2);
       class2.add(nextclass3);
       class2.add(nextclass4);
       class3.add(nextclass5);
       class3.add(nextclass6);
       class4.add(nextclass7);
       class4.add(nextclass8);
       nextclass1.add(txt1);
       nextclass1.add(txt2);
       nextclass2.add(txt3);
       nextclass2.add(txt4);
       nextclass3.add(txt5);
       nextclass3.add(txt6);
       nextclass4.add(txt7);
       nextclass4.add(txt8);
       nextclass5.add(txt9);
       nextclass5.add(txt10);
       nextclass6.add(txt11);
       nextclass6.add(txt12);
       nextclass7.add(txt13);
       nextclass7.add(txt14);
       nextclass8.add(txt15);
       nextclass8.add(txt16);
       tree=new JTree(root);                                   //用root做根的树组件
       tree.addTreeSelectionListener(this);                    //监视树组件上的事件
       showText=new JTextArea();
       setLayout(new GridLayout(1,2)); 
       add(new JScrollPane(tree));
       add(new JScrollPane(showText));
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
       setBounds(80,80,300,300);
       validate();
    }
    public void valueChanged(TreeSelectionEvent e){
       DefaultMutableTreeNode node=
       (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
       if(node.isLeaf()){
           Goods s=(Goods)node.getUserObject();                //得到节点中存放的对象
           showText.append(s.name+","+s.txt+"元\n");  
       }
       else{
           showText.setText(null);
       }   
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值