求助:如何在动态生成的树添加右键菜单,给提示者都有分,先谢谢了 我已经建成三级目录树了,我想问右击树接点时如何才能出现右键菜单.请大家帮帮忙,实在是没有一点头绪啊. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //右键点击事件jPopupMenu = new JPopupMenu();JMenuItem item1 = new JMenuItem("添加子节点");JMenuItem item2 = new JMenuItem("编辑节点");JMenuItem item3 = new JMenuItem("删除节点");jPopupMenu.add(item1);jPopupMenu.add(item2);jPopupMenu.add(item3);item1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ae) {addNode();}});tree.add(jPopupMenu);tree.addMouseListener(new MouseAdapter() {public void mouseReleased(MouseEvent er) {try {//是否右键单击if (er.getClickCount() == 1 && SwingUtilities.isRightMouseButton(er)) {TreePath path = tree.getPathForLocation(er.getX(), er.getY());if (path == null)return;tree.setSelectionPath(path);jPopupMenu.show(tree, er.getX(), er.getY());}else if (er.getClickCount() == 1) {TreePath path = tree.getPathForLocation(er.getX(), er.getY());if (path == null)return;if (tree.isExpanded(path)) {tree.collapsePath(path);}else {tree.expandPath(path);}return;}}catch (Exception ex) {ex.printStackTrace();}}}); 谢谢你 :mituzhishi(向往未来)! Integer[] 转 String[] 数组 运行时错误 java中有没有提供获取操作系统语言的API? 请问大家做GUI的 SWING组件添加 和 界面布局 时候都是自己写代码吗?还是用JBUILDER这种IDE啊? 那里有WINHELP格式的JAVA Document下载? 为什么我的程序打包成jar后就不能访问ORACLE? 急求jb7的注册码 菜鸟问问关于JBuilder的问题 我现在编译了一个CLASS文件????? Date.getTime()问题 一个画图程序的一点问题 如何在目录树上建菜单
JMenuItem item1 = new JMenuItem("添加子节点");
JMenuItem item2 = new JMenuItem("编辑节点");
JMenuItem item3 = new JMenuItem("删除节点");jPopupMenu.add(item1);
jPopupMenu.add(item2);
jPopupMenu.add(item3);item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
addNode();
}
});
tree.add(jPopupMenu);
tree.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent er) {
try {
//是否右键单击
if (er.getClickCount() == 1 && SwingUtilities.isRightMouseButton(er)) {
TreePath path = tree.getPathForLocation(er.getX(), er.getY());
if (path == null)
return;
tree.setSelectionPath(path);
jPopupMenu.show(tree, er.getX(), er.getY());
}else if (er.getClickCount() == 1) {
TreePath path = tree.getPathForLocation(er.getX(), er.getY());
if (path == null)
return;
if (tree.isExpanded(path)) {
tree.collapsePath(path);
}
else {
tree.expandPath(path);
}
return;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});