在java中如何响应单击JTree树中一个节点?
请诸位帮个忙!我在java中建立了一个JTree的树,我想完成的功能就是,单击某一个节点后,响应对映的操作。packagemyawt;importjava.awt.*;i...
请诸位帮个忙!
我在java中建立了一个JTree的树,我想完成的功能就是,单击某一个节点后,响应对映的操作。
package myawt;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class MyF2 extends JFrame implements TreeSelectionListener{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("学校");
DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("教学部");
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("行政部");
JTree tree1=new JTree(root);
public MyF2(){
super("JTree");
this.setSize(800, 600);
root.add(root1);
root.add(root2);
this.add(tree1);
this.setVisible(true);
}
public static void main(String[] args) {
new MyF2();
}
public void valueChanged(TreeSelectionEvent e) {
我只知道要写这样一个方法,但是如何写我不会
例如,单击(或双击)“教学部”这个节点,就会输出“教学部”这几个字。
}
}
请大家帮帮忙~!谢谢! 展开
我在java中建立了一个JTree的树,我想完成的功能就是,单击某一个节点后,响应对映的操作。
package myawt;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class MyF2 extends JFrame implements TreeSelectionListener{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("学校");
DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("教学部");
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("行政部");
JTree tree1=new JTree(root);
public MyF2(){
super("JTree");
this.setSize(800, 600);
root.add(root1);
root.add(root2);
this.add(tree1);
this.setVisible(true);
}
public static void main(String[] args) {
new MyF2();
}
public void valueChanged(TreeSelectionEvent e) {
我只知道要写这样一个方法,但是如何写我不会
例如,单击(或双击)“教学部”这个节点,就会输出“教学部”这几个字。
}
}
请大家帮帮忙~!谢谢! 展开
2个回答
展开全部
我给你写了一个例子 你看看吧 你看了就明白了
package tree;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.beancontext.BeanContext;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;
public class MainFrame extends JFrame {
public static void main(String args[]) {
try {
MainFrame frame = new MainFrame();
Dimension frameSize = frame.getSize();
Dimension screenSize = 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 (Exception e) {
e.printStackTrace();
}
}
private JTree tree;
private DefaultMutableTreeNode root = new DefaultMutableTreeNode("语言");
private DefaultMutableTreeNode menu_accp = new DefaultMutableTreeNode("英语");
private DefaultMutableTreeNode menu_benet = new DefaultMutableTreeNode("法语");
private DefaultMutableTreeNode menu_accp_s1 = new DefaultMutableTreeNode("4级");
private DefaultMutableTreeNode menu_accp_s2 = new DefaultMutableTreeNode("6级");
private DefaultMutableTreeNode menu_accp_y2 = new DefaultMutableTreeNode("8级");
private JTextField txtParent;
private JTextField txtNode;
private JTextField txtType;
private JTextField txtChild;
private JPopupMenu popupMenu;
/**
* Create the frame
*/
public MainFrame() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 535, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(24, 10, 173, 317);
getContentPane().add(scrollPane);
tree = new JTree(root);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getModifiers()==Event.META_MASK){
addPopup(getContentPane(), popupMenu);
popupMenu.show(getContentPane(),e.getX()+30,e.getY()+40);
}
}
});
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
Object nodeInfo = 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 = new JTextField();
txtParent.setBounds(339, 49, 90, 21);
getContentPane().add(txtParent);
txtNode = new JTextField();
txtNode.setBounds(339, 107, 90, 21);
getContentPane().add(txtNode);
txtType = new JTextField();
txtType.setBounds(339, 167, 90, 21);
getContentPane().add(txtType);
txtChild = new JTextField();
txtChild.setBounds(339, 231, 90, 21);
getContentPane().add(txtChild);
final JLabel label = new JLabel();
label.setText("父类型节点");
label.setBounds(225, 49, 90, 15);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("节点的名称");
label_1.setBounds(225, 107, 90, 15);
getContentPane().add(label_1);
final JLabel label_2 = new JLabel();
label_2.setText("节点的类型");
label_2.setBounds(225, 167, 90, 15);
getContentPane().add(label_2);
final JLabel label_3 = new JLabel();
label_3.setText("子节点的个数");
label_3.setBounds(225, 231, 90, 15);
getContentPane().add(label_3);
popupMenu = new JPopupMenu();
final JMenuItem menuItem = new JMenuItem();
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.removeNodeFromParent(node);
}
});
menuItem.setText("删除");
popupMenu.add(menuItem);
//
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
package tree;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.beancontext.BeanContext;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;
public class MainFrame extends JFrame {
public static void main(String args[]) {
try {
MainFrame frame = new MainFrame();
Dimension frameSize = frame.getSize();
Dimension screenSize = 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 (Exception e) {
e.printStackTrace();
}
}
private JTree tree;
private DefaultMutableTreeNode root = new DefaultMutableTreeNode("语言");
private DefaultMutableTreeNode menu_accp = new DefaultMutableTreeNode("英语");
private DefaultMutableTreeNode menu_benet = new DefaultMutableTreeNode("法语");
private DefaultMutableTreeNode menu_accp_s1 = new DefaultMutableTreeNode("4级");
private DefaultMutableTreeNode menu_accp_s2 = new DefaultMutableTreeNode("6级");
private DefaultMutableTreeNode menu_accp_y2 = new DefaultMutableTreeNode("8级");
private JTextField txtParent;
private JTextField txtNode;
private JTextField txtType;
private JTextField txtChild;
private JPopupMenu popupMenu;
/**
* Create the frame
*/
public MainFrame() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 535, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(24, 10, 173, 317);
getContentPane().add(scrollPane);
tree = new JTree(root);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getModifiers()==Event.META_MASK){
addPopup(getContentPane(), popupMenu);
popupMenu.show(getContentPane(),e.getX()+30,e.getY()+40);
}
}
});
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
Object nodeInfo = 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 = new JTextField();
txtParent.setBounds(339, 49, 90, 21);
getContentPane().add(txtParent);
txtNode = new JTextField();
txtNode.setBounds(339, 107, 90, 21);
getContentPane().add(txtNode);
txtType = new JTextField();
txtType.setBounds(339, 167, 90, 21);
getContentPane().add(txtType);
txtChild = new JTextField();
txtChild.setBounds(339, 231, 90, 21);
getContentPane().add(txtChild);
final JLabel label = new JLabel();
label.setText("父类型节点");
label.setBounds(225, 49, 90, 15);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("节点的名称");
label_1.setBounds(225, 107, 90, 15);
getContentPane().add(label_1);
final JLabel label_2 = new JLabel();
label_2.setText("节点的类型");
label_2.setBounds(225, 167, 90, 15);
getContentPane().add(label_2);
final JLabel label_3 = new JLabel();
label_3.setText("子节点的个数");
label_3.setBounds(225, 231, 90, 15);
getContentPane().add(label_3);
popupMenu = new JPopupMenu();
final JMenuItem menuItem = new JMenuItem();
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.removeNodeFromParent(node);
}
});
menuItem.setText("删除");
popupMenu.add(menuItem);
//
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
展开全部
xxxTree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
if (node.getUserObject() instanceof User) {
User selectUser= (User) node
.getUserObject();
selectUser(selectUser);// 选择后需要执行的代码
}
}
});
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询