日期:2014-05-20  浏览次数:20830 次

关于JTREE的问题~~~~~~~~~
在删除树的节点的时候老是出问题!!!!!!!!!
代码如下:
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   javax.swing.tree.*;
import   javax.swing.event.*;
class   JTreeTest   extends   JFrame
{
Container   cn;


JPopupMenu   jpm=new   JPopupMenu();

JPopupMenu   jpm1=new   JPopupMenu();

JMenu   jm=new   JMenu( "编辑 ");
JMenuItem   jmi1=new   JMenuItem( "编辑 ");
JMenuItem   jmi2=new   JMenuItem( "删除 ");
JMenuItem   jmi3=new   JMenuItem( "退出 ");


 
JMenuItem   jmi4=new   JMenuItem( "锁定 ");
JMenuItem   jmi5=new   JMenuItem( "解锁 ");




DefaultMutableTreeNode   root=new   DefaultMutableTreeNode( "JTree ");

DefaultMutableTreeNode   colors=new   DefaultMutableTreeNode( "colors ");

DefaultMutableTreeNode   sports=new   DefaultMutableTreeNode( "sports ");

DefaultMutableTreeNode   basketball=new   DefaultMutableTreeNode( "basketball ");
DefaultMutableTreeNode   football=new   DefaultMutableTreeNode( "football ");
DefaultMutableTreeNode   hockey=new   DefaultMutableTreeNode( "hockey ");

DefaultMutableTreeNode   food=new   DefaultMutableTreeNode( "food ");

DefaultMutableTreeNode   hotdog=new   DefaultMutableTreeNode( "hotdog ");
DefaultMutableTreeNode   pizza=new   DefaultMutableTreeNode( "pizza ");
DefaultMutableTreeNode   ravioli=new   DefaultMutableTreeNode( "ravioli ");
DefaultMutableTreeNode   banana=new   DefaultMutableTreeNode( "banana ");

JTree   tree;

JButton   btn1=new   JButton( "删除 ");
JButton   btn2=new   JButton( "退出 ");

DefaultMutableTreeNode   node;
JTreeTest()
{
cn=this.getContentPane();
cn.setLayout(null);
tree=new   JTree(root);
tree.setBounds(50,20,240,280);
btn2.setBounds(180,320,80,30);
btn1.setBounds(80,320,80,30);


root.add(colors);

root.add(sports);
sports.add(basketball);
sports.add(football);
sports.add(hockey);

root.add(food);
food.add(hotdog);
food.add(pizza);
food.add(ravioli);
food.add(banana);

cn.add(tree);
cn.add(btn1);
cn.add(btn2);

jpm.add(jm);
jpm.add(jmi2);
jpm.add(jmi3);


jm.add(jmi4);
jm.add(jmi5);


cn.addMouseListener(new   MouseAdapter()
{
public   void   mouseClicked(MouseEvent   e)
{
cn.add(jpm);
jpm.show(cn,e.getX(),e.getY());
}
}
);
jmi3.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
System.exit(0);
}
}
);
jmi4.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
tree.setEnabled(false);
}
});
jmi5.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
tree.setEnabled(true);
}
});