package com.MyQQ.test;import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;/**
 * @author 
 * 2012-4-9
 */
public class ShowFriend {

JFrame  showfriend=new JFrame();
String  string;



public  void  init(String  string){
//public  void  init(){
showfriend.setBounds(800, 50, 290,716);
showfriend.setBackground(Color.blue);
JPanel  p1=new  JPanel();


JLabel  label1=new  JLabel();
JLabel  label2=new  JLabel();
JLabel  label3=new  JLabel();
JLabel  label4=new  JLabel();

JTextField  tf1=new  JTextField(15);

JTextField  tf2=new  JTextField(25);
JButton  btn1=new  JButton();
//让label2显示网名
String  strNick;
  strNick=new QQDB().showNick(string);
        label2.setText( strNick);


tf1.setText("编辑个性签名");
tf2.setText("请输入你要查询的内容");
label1.setIcon(new  ImageIcon(".\\src\\image\\headlog.jpg"));
label3.setIcon(new  ImageIcon(".\\src\\image\\weather.png"));
label4.setIcon(new  ImageIcon(".\\src\\image\\QQZone.png"));
btn1.setIcon(new ImageIcon(".\\src\\image\\search.png"));

//想要在这个panel里用setbounds必须先把p1的布局设置空
//各个组件如果都在一个面板中以左上角为起始点
//为各个组件设置布局
p1.setLayout(null);
label1.setBounds(0, 16, 79, 81);
label1.setBorder(null);
label2.setBounds(91,16, 124, 20);
tf1.setBounds(91, 45, 128, 20);
label3.setBounds(225, 16, 56, 54);
label4.setBounds(91, 76, 189, 22);
tf2.setBounds(0, 106, 240, 25);
btn1.setBounds(243, 106, 32, 22);
btn1.setBorder(null);
btn1.addActionListener(new  MyAction());


//JTabbedPane允许用户通过单击具有给定标题和/或图标的选项卡,在一组组件之间进行切换

JTabbedPane  tPane=new  JTabbedPane(JTabbedPane.TOP);

tPane.setBounds(0, 126, 290, 543);

   String  str3=new QQDB().getTableName(string);

  //创建树,设置node为这个树的根结点,并且名字为root
System.out.println("1");
  final DefaultMutableTreeNode  node=new  DefaultMutableTreeNode("root");
  final JTree   tree=new  JTree(node);
System.out.println("2");
  //调用数据库里面的添加子节点的方法添加节点到根下面
  new QQDB().createNode(node, "0",str3);
  System.out.println("4");
//把父子结点之间的连线去掉
  tree.putClientProperty("JTree.lineStyle", "None");
  //tree.setRootVisible(false);
  //tree.putClientProperty("JTree.lineStyle", "Angled"); 用来保留连线,其实默认值也是保留连线的
      //tree.setShowsRootHandles(true);  用来添加把手
  //tree.setRootVisible(false);
  
  

//JScrollPane 管理视口、可选的垂直和水平滚动条以及可选的行和列标题视口
JScrollPane  jsp1=new  JScrollPane(tree);
JScrollPane  jsp2=new  JScrollPane();
JScrollPane  jsp3=new  JScrollPane();
JScrollPane  jsp4=new  JScrollPane();
JScrollPane  jsp5=new  JScrollPane();


//添加jscrollpanel并且为它们添加图标
tPane.addTab(null,new ImageIcon(".\\src\\image\\1.jpg"),jsp1);
tPane.addTab(null,new ImageIcon(".\\src\\image\\2.jpg"),jsp2);
tPane.addTab(null,new ImageIcon(".\\src\\image\\3.jpg"),jsp3);
tPane.addTab(null,new ImageIcon(".\\src\\image\\4.jpg"),jsp4);
tPane.addTab(null,new ImageIcon(".\\src\\image\\5.jpg"),jsp5);
tPane.setBorder(null);
jsp1.setBorder(null);
jsp2.setBorder(null);
jsp3.setBorder(null);
jsp4.setBorder(null);
jsp5.setBorder(null);


MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                int selRow = tree.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
                if(selRow != -1) {
                    if(e.getClickCount() == 1) {
                        mySingleClick(selRow, selPath);
                    }
                    else if(e.getClickCount() == 2) {
                        myDoubleClick(selRow, selPath);
                        
                    }
                }
            } private void myDoubleClick(int selRow, TreePath selPath) {
//判断是不是最后的节点,用路径值看是不是数据库里面要求的第三级
if (selPath.getPathCount()==3) 
{
    
???????????这里怎么拿到叶子的ID呢,现在可以拿到的只是name
//把当前节点的Id传递进去
new DialogJFrame();
//selPath.getLastPathComponent()得到的是节点name,通过这个name去查ID
System.out.println(selPath.getLastPathComponent());
System.out.println(selPath);
System.out.println("--------------");

}else {
//打开下面的子节点,不用写


}

} private void mySingleClick(int selRow, TreePath selPath) {

}
        };
        tree.addMouseListener(ml);


 

//p1.setBackground(Color.BLUE);
p1.add(label1);
p1.add(label2);
p1.add(label3);
p1.add(label4);
p1.add(tf1);
p1.add(tf2);
p1.add(btn1);
p1.add(tPane);

showfriend.add(p1);

showfriend.setVisible(true);
showfriend.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
//public static void main(String[] args) {

 public void showfriend(String str){
     this.string=str;
     //为了显示网名,我们把ID一直传下去,便于数据库的查询
     init(string);
     //new ShowFriend();
    }
}//打开网页进行搜索
class   MyAction   implements  ActionListener{ public void actionPerformed(ActionEvent e) {
 try {
   Process process = Runtime.getRuntime().exec("explorer http://www.baidu.com");
  } catch (IOException e3) {
 e3.printStackTrace();
  }
 }

}
数据库操作如下
//创建树遍历表里面的节点

public void createNode(DefaultMutableTreeNode parent, String ParentId,String   string){

try {
                  
 this.str1=string;
 
 
if (str1.equals("adminfriend")) {
DefaultMutableTreeNode  temp=null;

String  sql="select  friendId,name,ParentId  from  adminfriend  where  ParentID='" + ParentId + "'"; 

stmt=conn.createStatement();
res=stmt.executeQuery(sql);

//定义光标位置
int i=0;

while (res.next()) {
i++;
   temp=new DefaultMutableTreeNode(res.getString("name"));
   parent.add(temp);
   createNode(temp, res.getString("friendId"),string);
   res=stmt.executeQuery(sql);
   res.relative(i);

}
}if (string.equals("rootfriend")) {
DefaultMutableTreeNode  temp=null;

String  sql="select  friendId,name,ParentId  from  rootfriend  where  ParentID='" + ParentId + "'"; 

stmt=conn.createStatement();
res=stmt.executeQuery(sql);



//定义光标位置
int i=0;

while (res.next()) {
i++;
   temp=new DefaultMutableTreeNode(res.getString("name"));
   parent.add(temp);
   createNode(temp, res.getString("friendId"),string);
   res=stmt.executeQuery(sql);
   res.relative(i);

}
}



} catch (Exception e) {
e.printStackTrace();
}


}数据库如图