代码如下:
package fy;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.tree.*;
import java.lang.*;
import javax.swing.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Frame1 extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JTree jTree1 = new JTree();
private JButton jButton1 = new JButton();
private JPopupMenu jPopupMenu1 = new JPopupMenu();
private JMenuItem jMenuItem1 = new JMenuItem();
private JMenuItem jMenuItem2 = new JMenuItem();
private JMenuItem jMenuItem3 = new JMenuItem();
private JMenuItem jMenuItem4 = new JMenuItem();
private JMenu jMenu1 = new JMenu();
private JMenuItem jMenuItem5 = new JMenuItem();
private JMenuItem jMenuItem6 = new JMenuItem();
private JMenuItem jMenuItem7 = new JMenuItem();
private JMenuItem jMenuItem8 = new JMenuItem(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.addKeyListener(new java.awt.event.KeyAdapter() { }); jPanel1.setLayout(xYLayout1);
jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setToolTipText("");
jPanel1.setToolTipText("");
jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
//public void mouseClicked(MouseEvent e) {
//jPanel1_mouseClicked(e);
//}
});
jPopupMenu1.setInvoker(this);
jPopupMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jPopupMenu1_mouseClicked(e);
}
});
jMenuItem1.setText("添加");
jMenuItem2.setText("删除");
jMenuItem3.setText("刷新");
jMenuItem4.setText("重命名");
jMenu1.setText("新建");
jMenuItem5.setText("子节点");
jMenuItem6.setText("变量");
jMenuItem7.setText("属性");
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTree1, new XYConstraints(20, 16, 309, 224));
jPanel1.add(jButton1, new XYConstraints(196, 259, 81, 30));
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
jPopupMenu1.add(jMenuItem4);
jPopupMenu1.add(jMenu1);
jPopupMenu1.addSeparator();
jMenu1.add(jMenuItem5);
jMenu1.add(jMenuItem6);
jTree1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent me){
jPopupMenu1_mouseClicked(me);
}
public void mouseClicked(MouseEvent e) {
jTree1_mouseClicked(e);
}
});
jPopupMenu1.add(jMenuItem7);
jPopupMenu1.add(jMenuItem8); }
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_actionPerformed(ActionEvent e) { class Tdata /*二叉树的定义*/
{
int data;
Tdata child,brother;
}
class TreeLink /*工作链表的定义*/
{
Tdata Node;
DefaultMutableTreeNode a; /*定义节点a*/
TreeLink next; /*链指针*/
TreeLink()
{
Node=null;
next=null;
}
}
/*程序框架*/
TreeLink head,p;
Tdata data=new Tdata();
Tdata data1=new Tdata();
Tdata data2=new Tdata();
Tdata data3=new Tdata();
Tdata data4=new Tdata();
Tdata data5=new Tdata();
Tdata data6=new Tdata();
Tdata data7=new Tdata();
data.data=0;
data1.data=1;
data2.data=2;
data3.data=3;
data4.data=4;
data5.data=5;
data6.data=6;
data7.data=7;
//data1、data2、data3属于第一级
data1.child=data.child;
data.child=data1;
data2.brother=data1.brother;
data1.brother=data2;
data3.brother=data2.child;
data2.brother=data3;
//data4、data5是data2data2的child
data4.child=data2.child;
data2.child=data4;
data5.brother=data4.brother;
data4.brother=data5;
//data6是data3的child
data6.child=data3.child;
data3.child=data6;
//data7是data5的chlid
data7.child=data5.child;
data5.child=data7;head=new TreeLink(); /*实例化*/
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
DefaultTreeModel mod;
p=new TreeLink(); /*实例化*/
p.Node=data;
p.a=root; /*储存"root",用以实现二叉树和jtree的一一对应*/
p.next=head.next; /*将root点加入链表的指针变化*/
head.next=p; /*将root点加入链表的指针变化*/
while(head.next!=null){
p=head.next; /*取链表的头节点*/
head.next=p.next; /*取链表的头节点*/
Tdata node=p.Node;
node=node.child;
while(node!=null){
DefaultMutableTreeNode no=new DefaultMutableTreeNode(String.valueOf(node.data));
/*将no以文件夹的形式画在root的下一级*/
p.a.add(no);
TreeLink tt=new TreeLink();
tt.Node=node;
tt.a=no;
tt.next=head.next;
head.next=tt;
node=node.brother;
}
}
mod=new DefaultTreeModel(root);
jTree1.setModel(mod);
} void jPopupMenu1_mouseClicked(MouseEvent e) {
if(e.getModifiers()==Event.META_MASK){
jPopupMenu1.show(jTree1,e.getX(),e.getY());
}
} void jTree1_mouseClicked(MouseEvent e) {
???????
}
}
package fy;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.tree.*;
import java.lang.*;
import javax.swing.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Frame1 extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JTree jTree1 = new JTree();
private JButton jButton1 = new JButton();
private JPopupMenu jPopupMenu1 = new JPopupMenu();
private JMenuItem jMenuItem1 = new JMenuItem();
private JMenuItem jMenuItem2 = new JMenuItem();
private JMenuItem jMenuItem3 = new JMenuItem();
private JMenuItem jMenuItem4 = new JMenuItem();
private JMenu jMenu1 = new JMenu();
private JMenuItem jMenuItem5 = new JMenuItem();
private JMenuItem jMenuItem6 = new JMenuItem();
private JMenuItem jMenuItem7 = new JMenuItem();
private JMenuItem jMenuItem8 = new JMenuItem(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.addKeyListener(new java.awt.event.KeyAdapter() { }); jPanel1.setLayout(xYLayout1);
jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setToolTipText("");
jPanel1.setToolTipText("");
jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
//public void mouseClicked(MouseEvent e) {
//jPanel1_mouseClicked(e);
//}
});
jPopupMenu1.setInvoker(this);
jPopupMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jPopupMenu1_mouseClicked(e);
}
});
jMenuItem1.setText("添加");
jMenuItem2.setText("删除");
jMenuItem3.setText("刷新");
jMenuItem4.setText("重命名");
jMenu1.setText("新建");
jMenuItem5.setText("子节点");
jMenuItem6.setText("变量");
jMenuItem7.setText("属性");
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTree1, new XYConstraints(20, 16, 309, 224));
jPanel1.add(jButton1, new XYConstraints(196, 259, 81, 30));
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
jPopupMenu1.add(jMenuItem4);
jPopupMenu1.add(jMenu1);
jPopupMenu1.addSeparator();
jMenu1.add(jMenuItem5);
jMenu1.add(jMenuItem6);
jTree1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent me){
jPopupMenu1_mouseClicked(me);
}
public void mouseClicked(MouseEvent e) {
jTree1_mouseClicked(e);
}
});
jPopupMenu1.add(jMenuItem7);
jPopupMenu1.add(jMenuItem8); }
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_actionPerformed(ActionEvent e) { class Tdata /*二叉树的定义*/
{
int data;
Tdata child,brother;
}
class TreeLink /*工作链表的定义*/
{
Tdata Node;
DefaultMutableTreeNode a; /*定义节点a*/
TreeLink next; /*链指针*/
TreeLink()
{
Node=null;
next=null;
}
}
/*程序框架*/
TreeLink head,p;
Tdata data=new Tdata();
Tdata data1=new Tdata();
Tdata data2=new Tdata();
Tdata data3=new Tdata();
Tdata data4=new Tdata();
Tdata data5=new Tdata();
Tdata data6=new Tdata();
Tdata data7=new Tdata();
data.data=0;
data1.data=1;
data2.data=2;
data3.data=3;
data4.data=4;
data5.data=5;
data6.data=6;
data7.data=7;
//data1、data2、data3属于第一级
data1.child=data.child;
data.child=data1;
data2.brother=data1.brother;
data1.brother=data2;
data3.brother=data2.child;
data2.brother=data3;
//data4、data5是data2data2的child
data4.child=data2.child;
data2.child=data4;
data5.brother=data4.brother;
data4.brother=data5;
//data6是data3的child
data6.child=data3.child;
data3.child=data6;
//data7是data5的chlid
data7.child=data5.child;
data5.child=data7;head=new TreeLink(); /*实例化*/
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
DefaultTreeModel mod;
p=new TreeLink(); /*实例化*/
p.Node=data;
p.a=root; /*储存"root",用以实现二叉树和jtree的一一对应*/
p.next=head.next; /*将root点加入链表的指针变化*/
head.next=p; /*将root点加入链表的指针变化*/
while(head.next!=null){
p=head.next; /*取链表的头节点*/
head.next=p.next; /*取链表的头节点*/
Tdata node=p.Node;
node=node.child;
while(node!=null){
DefaultMutableTreeNode no=new DefaultMutableTreeNode(String.valueOf(node.data));
/*将no以文件夹的形式画在root的下一级*/
p.a.add(no);
TreeLink tt=new TreeLink();
tt.Node=node;
tt.a=no;
tt.next=head.next;
head.next=tt;
node=node.brother;
}
}
mod=new DefaultTreeModel(root);
jTree1.setModel(mod);
} void jPopupMenu1_mouseClicked(MouseEvent e) {
if(e.getModifiers()==Event.META_MASK){
jPopupMenu1.show(jTree1,e.getX(),e.getY());
}
} void jTree1_mouseClicked(MouseEvent e) {
???????
}
}
node是你当前点击的节点。
在树中加一个事件来使用这个函数就可以得到
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;public class TreeDemo6 implements ActionListener,TreeModelListener
{
JLabel label = null;
JTree tree = null;
DefaultTreeModel treeModel = null;
String nodeName = null; //原有节点名称
public TreeDemo6()
{
JFrame f = new JFrame("TreeDemo");
Container contentPane = f.getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");
tree = new JTree(root);
tree.setEditable(true);
tree.addMouseListener(new MouseHandle());
treeModel = (DefaultTreeModel)tree.getModel();
treeModel.addTreeModelListener(this);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
JPanel panel = new JPanel();
JButton b = new JButton("新增节点");
b.addActionListener(this);
panel.add(b);
label = new JLabel("Action");
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); }
public void actionPerformed(ActionEvent ae)
{
if (ae.getActionCommand().equals("新增节点"))
{
DefaultMutableTreeNode parentNode = null;
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新节点");
newNode.setAllowsChildren(true);
TreePath parentPath = tree.getSelectionPath(); parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent()); treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());
tree.scrollPathToVisible(new TreePath(newNode.getPath()));
label.setText("新增节点成功");
}
}
public void treeNodesChanged(TreeModelEvent e) {
TreePath treePath = e.getTreePath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
try {
int[] index = e.getChildIndices();
node = (DefaultMutableTreeNode)node.getChildAt(index[0]);
} catch (NullPointerException exc) {}
label.setText(nodeName+" 更改数据为: "+(String)node.toString());//这个就是你想得到的节点名称
}
public void treeNodesInserted(TreeModelEvent e) {
System.out.println("new node inserted");
}
public void treeNodesRemoved(TreeModelEvent e) {
System.out.println("node deleted");
}
public void treeStructureChanged(TreeModelEvent e) {
System.out.println("structrue changed");
} public static void main(String args[]) {
new TreeDemo6();
}
class MouseHandle extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
try{
JTree tree = (JTree)e.getSource();
int rowLocation = tree.getRowForLocation(e.getX(), e.getY());
TreePath treepath = tree.getPathForRow(rowLocation);
TreeNode treenode = (TreeNode) treepath.getLastPathComponent();
nodeName = treenode.toString();
}catch(NullPointerException ne){}
}
}
}