日期:2014-05-20 浏览次数:20869 次
JTree fTree = new JTree(); fTree.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { JTree tree = (JTree) e.getComponent(); TreePath path = tree.getPathForLocation(e.getX(), e.getY()); tree.setSelectionPath(path); if (path == null) { System.out.println("Not click on the node..."); return; } if (path.getParentPath() != null) { JMenuItem menu1 = new JMenuItem("menu1"); JMenuItem menu2 = new JMenuItem("menu2"); JMenuItem menu3 = new JMenuItem("menu3"); JMenuItem menu4 = new JMenuItem("menu4"); JMenuItem menu5 = new JMenuItem("menu5"); JPopupMenu popupMenu1; popupMenu1 = new JPopupMenu(); popupMenu1.add(menu3); popupMenu1.add(menu4); popupMenu1.add(menu5); popupMenu1.add(menu1); popupMenu1.add(menu2); menu1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); popupMenu1.show(tree, e.getX(), e.getY()); } } } });
------解决方案--------------------
swing嘛,果断帮顶