Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1119574
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: Java

2010-08-05 13:12:49

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class test extends JApplet{
    JTree tree;
    JTextField jtf;
    public void init(){
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
        
        DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
        top.add(a);
        DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
        a.add(a1);
        DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
        a.add(a2);
        

        DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
        top.add(b);
        DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
        b.add(b1);
        DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
        b.add(b2);
        
        tree = new JTree(top);
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        JScrollPane jsp = new JScrollPane(tree,v,h);
        contentPane.add(jsp, BorderLayout.CENTER);
        jtf = new JTextField("", 20);
        contentPane.add(jtf, BorderLayout.SOUTH);
        
        tree.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent me){
                doMouseclicked(me);
            }
        });
    }
    void doMouseclicked(MouseEvent me){
        TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
        if(tp !=null)
            jtf.setText(tp.toString());
        else
            jtf.setText("");
    }

}


阅读(594) | 评论(0) | 转发(0) |
0

上一篇:java-数据报

下一篇:swing-2-Icon+button+text

给主人留下些什么吧!~~