在java中如何响应单击JTree树中一个节点?

 我来答
匿名用户
2015-12-29
展开全部
我给你写了一个例子你看看吧你看了就明白了

packagetree;

importjava.awt.Component;
importjava.awt.Dimension;
importjava.awt.Event;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.beans.beancontext.BeanContext;

importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JMenuItem;
importjavax.swing.JPopupMenu;
importjavax.swing.JScrollPane;
importjavax.swing.JTextField;
importjavax.swing.JTree;
importjavax.swing.event.TreeSelectionEvent;
importjavax.swing.event.TreeSelectionListener;
importjavax.swing.table.DefaultTableModel;
importjavax.swing.tree.DefaultMutableTreeNode;
importjavax.swing.tree.DefaultTreeModel;

importcom.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;

publicclassMainFrameextendsJFrame{

publicstaticvoidmain(Stringargs[]){
try{
MainFrameframe=newMainFrame();
DimensionframeSize=frame.getSize();
DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}

frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);

frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}

privateJTreetree;
privateDefaultMutableTreeNoderoot=newDefaultMutableTreeNode("语言");
privateDefaultMutableTreeNodemenu_accp=newDefaultMutableTreeNode("英语");
privateDefaultMutableTreeNodemenu_benet=newDefaultMutableTreeNode("法语");
privateDefaultMutableTreeNodemenu_accp_s1=newDefaultMutableTreeNode("4级");
privateDefaultMutableTreeNodemenu_accp_s2=newDefaultMutableTreeNode("6级");
privateDefaultMutableTreeNodemenu_accp_y2=newDefaultMutableTreeNode("8级");

privateJTextFieldtxtParent;
privateJTextFieldtxtNode;
privateJTextFieldtxtType;
privateJTextFieldtxtChild;
privateJPopupMenupopupMenu;

/**
*Createtheframe
*/
publicMainFrame(){
super();
getContentPane().setLayout(null);
setBounds(100,100,535,375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

finalJScrollPanescrollPane=newJScrollPane();
scrollPane.setBounds(24,10,173,317);
getContentPane().add(scrollPane);
tree=newJTree(root);
tree.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(e.getModifiers()==Event.META_MASK){
addPopup(getContentPane(),popupMenu);
popupMenu.show(getContentPane(),e.getX()+30,e.getY()+40);
}
}
});
tree.addTreeSelectionListener(newTreeSelectionListener(){
publicvoidvalueChanged(TreeSelectionEvente){
DefaultMutableTreeNodenode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
ObjectnodeInfo=node.getUserObject();

if(node.isRoot()){
txtParent.setText("");
txtNode.setText(nodeInfo.toString());
txtType.setText("根节点");
txtChild.setText(node.getChildCount()+"");
}else{
txtParent.setText(node.getParent().toString());
txtNode.setText(nodeInfo.toString());
if(node.isLeaf()){
txtType.setText("叶节点");
}else{
txtType.setText("枝节点");
}
txtChild.setText(node.getChildCount()+"");
}

}
});
scrollPane.setViewportView(tree);

txtParent=newJTextField();
txtParent.setBounds(339,49,90,21);
getContentPane().add(txtParent);

txtNode=newJTextField();
txtNode.setBounds(339,107,90,21);
getContentPane().add(txtNode);

txtType=newJTextField();
txtType.setBounds(339,167,90,21);
getContentPane().add(txtType);

txtChild=newJTextField();
txtChild.setBounds(339,231,90,21);
getContentPane().add(txtChild);

finalJLabellabel=newJLabel();
label.setText("父类型节点");
label.setBounds(225,49,90,15);
getContentPane().add(label);

finalJLabellabel_1=newJLabel();
label_1.setText("节点的名称");
label_1.setBounds(225,107,90,15);
getContentPane().add(label_1);

finalJLabellabel_2=newJLabel();
label_2.setText("节点的类型");
label_2.setBounds(225,167,90,15);
getContentPane().add(label_2);

finalJLabellabel_3=newJLabel();
label_3.setText("子节点的个数");
label_3.setBounds(225,231,90,15);
getContentPane().add(label_3);

popupMenu=newJPopupMenu();

finalJMenuItemmenuItem=newJMenuItem();
menuItem.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
DefaultMutableTreeNodenode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
DefaultTreeModelmodel=(DefaultTreeModel)tree.getModel();
model.removeNodeFromParent(node);
}
});
menuItem.setText("删除");
popupMenu.add(menuItem);
//
}

privatestaticvoidaddPopup(Componentcomponent,finalJPopupMenupopup){
component.addMouseListener(newMouseAdapter(){
publicvoidmousePressed(MouseEvente){
if(e.isPopupTrigger())
showMenu(e);
}
publicvoidmouseReleased(MouseEvente){
if(e.isPopupTrigger())
showMenu(e);
}
privatevoidshowMenu(MouseEvente){
popup.show(e.getComponent(),e.getX(),e.getY());
}
});
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式