小弟初学java,做了一个JTree,右键菜单的重命名不知道咋做,那位高手帮忙看一下
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.*;
public class MyTree {
public MyTree() {
CreateTree frame=new CreateTree();
frame.setSize(400,320);
frame.setTitle("MyTree");
frame.setVisible(true); //设置窗体为可见
frame.setResizable(false);//设置窗体不可调整大小
}
public static void main(String[]args){
new MyTree();
}
}
class CreateTree extends JFrame{
JPanel pane1;
JPanel pane2;
JTree tree;
JScrollPane scroll;
JTextArea area;
JPopupMenu popupMenu;JPopupMenu popupMenu1;
JMenuItem copy,cut,paste;JMenuItem newName;
public CreateTree(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JbInit();
}
public void JbInit(){
pane1=(JPanel)getContentPane();
pane1.setLayout(new GridLayout(1,2));
DefaultMutableTreeNode top=new DefaultMutableTreeNode("我的电脑");
createTreeNode(top);//调用方法
tree=new JTree(top);
tree.addTreeExpansionListener(new TreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent e){//实现接口中的方法
}
public void treeExpanded(TreeExpansionEvent e){ //实现接口中的方法
TreePath path=e.getPath();
FileNode node=(FileNode)path.getLastPathComponent();
if(!node.isExplored())
{
DefaultTreeModel model=(DefaultTreeModel)tree.getModel(); //getModel返回正在提供数据的 TreeModel
node.explore();
model.nodeStructureChanged(node);//如果完全更改了节点的子节点、子节点的子节点、依此类推,则调用此方法
}
}
});
//JTree右键菜单
popupMenu1=new JPopupMenu();
popupMenu1.add(newName=new JMenuItem("重命名"));
newName.addActionListener(new ActionListener(){//设置菜单项动作侦听
public void actionPerformed(ActionEvent e){
}
});
tree.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu1.show(tree, e.getX(), e.getY());
}
}
});
//JTextArea右键菜单
popupMenu=new JPopupMenu();
popupMenu.add(copy=new JMenuItem("复制"));//菜单项
popupMenu.add(paste=new JMenuItem("粘贴"));
popupMenu.add(cut=new JMenuItem("剪切"));
copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));//设置快捷键
paste.setAccelerator(KeyStroke.getKeyStroke('V',InputEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.CTRL_MASK));
copy.addActionListener(new ActionListener(){//设置菜单项动作侦听
public void actionPerformed(ActionEvent e){
action(e);
}
});
paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
action(e);
}
});
cut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
action(e);
}
});
area=new JTextArea();
area.add(popupMenu);
area.addMouseListener(new MouseAdapter(){//设置鼠标监听
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {//是否右键
popupMenu.show(area, e.getX(), e.getY());//显示的位置
}
}
});
scroll=new JScrollPane(tree);
scroll.add(popupMenu);
pane1.add(scroll);
pane1.add(area);
}
public void action(ActionEvent e){
String str = e.getActionCommand();//返回与此动作相关的命令字符串
if (str.equals(copy.getText())) { // 复制
area.copy();
} else if (str.equals(paste.getText())) { // 粘贴
area.paste();
} else if (str.equals(cut.getText())) { // 剪切
area.cut();
}
}
public void createTreeNode(DefaultMutableTreeNode top){
File root=new File("");
File[] listFile=root.listRoots();//列出可用的文件系统根目录
for(int i=0;i<listFile.length;i++){
top.add(new FileNode(listFile[i]));//添加节点
}
}
}
class FileNode extends DefaultMutableTreeNode{
private boolean explored=false;
public FileNode(File file){
this.setUserObject(file);//将此节点的用户对象设置为 userObject
}
public boolean isLeaf(){
return !isDirectory();
}
public File getFile(){
return (File)getUserObject();//获得当前用户对象
}
public boolean isExplored(){
return explored;
}
public boolean isDirectory(){
File file=getFile();
return file.isDirectory();
}
public void explore(){
String str;
if(!isDirectory())
return;
if(!isExplored()){
File file=getFile();
File[] children=file.listFiles();//listFiles返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件
for(int i=0;i<children.length;++i)
{
add(new FileNode(children[i]));
explored = true;
}
}
}
}