主题:一个关于popupmenu的问题!在线等待答复!!!
[size=1][size=4]下面的一段程序 想在树的根节点上右键单击弹出快捷菜单
但问题是 每次必须先左键选中 然后再单击右键才能弹出快捷菜单
能不能像windows中 直接右键选中直接弹出快捷菜单
import java.awt.Container;
import java.awt.Frame;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class TreeDemo extends JFrame{
Frame f;
Container c;
JTree tree;
DefaultMutableTreeNode top,top1,top2;
DefaultTreeModel model;
PopupMenu popmenu;
MenuItem menu[]=new MenuItem[1];
public TreeDemo(){
super();
f=new Frame();
c=getContentPane();
popmenu=new PopupMenu();
menu[0]=new MenuItem("新建结点");
popmenu.add(menu[0]);
top=new DefaultMutableTreeNode("my computer");
model=new DefaultTreeModel(top);
tree=new JTree(model);
top1=new DefaultMutableTreeNode("c");
top2=new DefaultMutableTreeNode("d");
top.add(top1);
top.add(top2);
menu[0].addActionListener(new ActionListener(){ //右键快捷菜单
public void actionPerformed(ActionEvent e){
DefaultMutableTreeNode node=new DefaultMutableTreeNode("more");
model.insertNodeInto(node, top, 0);
}
});
tree.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(node.isRoot()&&(e.getModifiers()&InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK){
popmenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
c.add(popmenu);
c.add(tree);
f.add(c);
f.setSize(300,200);
f.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
TreeDemo t=new TreeDemo();
t.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
// TODO Auto-generated method stub
}
}[/size][/size]
但问题是 每次必须先左键选中 然后再单击右键才能弹出快捷菜单
能不能像windows中 直接右键选中直接弹出快捷菜单
import java.awt.Container;
import java.awt.Frame;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class TreeDemo extends JFrame{
Frame f;
Container c;
JTree tree;
DefaultMutableTreeNode top,top1,top2;
DefaultTreeModel model;
PopupMenu popmenu;
MenuItem menu[]=new MenuItem[1];
public TreeDemo(){
super();
f=new Frame();
c=getContentPane();
popmenu=new PopupMenu();
menu[0]=new MenuItem("新建结点");
popmenu.add(menu[0]);
top=new DefaultMutableTreeNode("my computer");
model=new DefaultTreeModel(top);
tree=new JTree(model);
top1=new DefaultMutableTreeNode("c");
top2=new DefaultMutableTreeNode("d");
top.add(top1);
top.add(top2);
menu[0].addActionListener(new ActionListener(){ //右键快捷菜单
public void actionPerformed(ActionEvent e){
DefaultMutableTreeNode node=new DefaultMutableTreeNode("more");
model.insertNodeInto(node, top, 0);
}
});
tree.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(node.isRoot()&&(e.getModifiers()&InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK){
popmenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
c.add(popmenu);
c.add(tree);
f.add(c);
f.setSize(300,200);
f.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
TreeDemo t=new TreeDemo();
t.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
// TODO Auto-generated method stub
}
}[/size][/size]