1.JTree怎么添加节点啊?(我的JTree是在netbeans中的Swing手动拖到面板的控件哦!)
2.JTree结构中的某一项如何点击右键弹出菜单模式;
我做的JTree是从Swing中拖到面板中的哦,谢谢!
------解决方案--------------------
netbeans没用过,我就给个用代码实现的吧
- Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;public class JTreeFrame extends JFrame{ private JTree tree; private DefaultTreeModel model; private DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); private DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("node1"); private DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("node2"); private DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("node3"); public JTreeFrame() { //添加各结点 root.add(node1); root.add(node2); node1.add(node3); model = new DefaultTreeModel(root); tree = new JTree(model); //添加监听 tree.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent event) { if (event.getButton() == MouseEvent.BUTTON3) { JPopupMenu popupMenu = new JPopupMenu(); JMenuItem itemShow = new JMenuItem("弹出窗口按钮"); popupMenu.add(itemShow); popupMenu.show(JTreeFrame.this, event.getX(), event.getY()); } } }); //添加JTree add(new JScrollPane(tree)); } public static void main(String[] args) { JFrame frame = new JTreeFrame(); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}