Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:JTree 和JPopupMenu事件? [Re:xinlan]
xinlan





发贴: 20
积分: 0
于 2005-08-07 22:11 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
像这样不是我要的
没有对节点进行区分


import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class TreeWithPopup extends JPanel {
DefaultMutableTreeNode root, node1, node2, node3;
public TreeWithPopup() {
MyJTree tree;
root = new DefaultMutableTreeNode("root", true);
node1 = new DefaultMutableTreeNode("node 1", true);
node2 = new DefaultMutableTreeNode("node 2" , true);
node3 = new DefaultMutableTreeNode("node 3", true);
root.add(node1);
node1.add(node2);
root.add(node3);
setLayout(new BorderLayout());
tree = new MyJTree(root);
add(new JScrollPane((JTree)tree),"Center");
}

public Dimension getPreferredSize(){
return new Dimension(300, 300);
}

public static void main(String s[]){
JFrame frame = new JFrame("Tree With Popup");
TreeWithPopup panel = new TreeWithPopup();

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel,"Center");

frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
frame.addWindowListener(new WindowCloser());
}
}

class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}

class MyJTree extends JTree implements ActionListener{
JPopupMenu popup;
JMenuItem mi;

MyJTree (DefaultMutableTreeNode dmtn) {
super(dmtn);
// define the popup
popup = new JPopupMenu();
mi = new JMenuItem("Insert a children");
mi.addActionListener(this);
mi.setActionCommand("insert");
popup.add(mi);
mi = new JMenuItem("Remove this node");
mi.addActionListener(this);
mi.setActionCommand("remove");
popup.add(mi);
popup.setOpaque(true);
popup.setLightWeightPopupEnabled(true);

addMouseListener (
new MouseAdapter () {
public void mouseReleased( MouseEvent e ) {
if ( e.isPopupTrigger()) {
popup.show( (JComponent)e.getSource(), e.getX(), e.getY() );
}
}
}
);

}
public void actionPerformed(ActionEvent ae) {
DefaultMutableTreeNode dmtn, node;

TreePath path = this.getSelectionPath();
dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
if (ae.getActionCommand().equals("insert")) {
node = new DefaultMutableTreeNode("children");
dmtn.add(node);
// thanks to Yong Zhang for the tip for refreshing the tree structure.
((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);
}
if (ae.getActionCommand().equals("remove")) {
node = (DefaultMutableTreeNode)dmtn.getParent();
node.removeAllChildren();
((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);
}
}
}


littledeer1974 edited on 2005-08-23 14:23


[转帖]面向对象的思维方法

话题树型展开
人气 标题 作者 字数 发贴时间
7761 JTree 和JPopupMenu事件? xinlan 36 2005-08-07 22:03
5749 Re:JTree 和JPopupMenu事件? xinlan 3097 2005-08-07 22:11
5428 Re:JTree 和JPopupMenu事件? kavinwang 39 2005-08-08 08:37
5759 Re:JTree 和JPopupMenu事件? xinlan 36 2005-08-09 10:19
5811 Re:JTree 和JPopupMenu事件? xinlan 1314 2005-08-09 10:50
5911 Re:JTree 和JPopupMenu事件? xinlan 35 2005-08-09 11:29

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923