package com.java.view; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.print.attribute.standard.Sides; import javax.swing.ButtonGroup; import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JProgressBar; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSlider; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.ListSelectionModel; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; 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 javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper; import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI; import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI.NormalColor; public class HelloWorld { private static TreePath path; public static void main(String[] args) { try { BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated; //UIManager.put("RootPane.setupButtonVisible", false); org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF(); } catch(Exception e) { //TODO exception } // 创建窗体对象 JFrame jFrame =new JFrame(); // 设置窗体大小 jFrame.setSize(800, 500); // 设置窗体全屏展示 //jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); // 设置窗体显示位置 //jFrame.setLocation(100,200); // 设置窗体显示正中间 jFrame.setLocationRelativeTo(null); // 设置窗体标题 jFrame.setTitle("窗体标题"); // 设置窗体不可全屏显示 //jFrame.setResizable(false); // 设置窗体关闭后退出程序 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置此窗口是否应该始终位于其他窗口上方 jFrame.setAlwaysOnTop(true); // 设置窗体图标 jFrame.setIconImage(new ImageIcon(HelloWorld.class.getResource("/images/book.png")).getImage()); JPanel jPanel =new JPanel(null); DefaultMutableTreeNode root=new DefaultMutableTreeNode("河南"); DefaultMutableTreeNode a=new DefaultMutableTreeNode("郑州"); DefaultMutableTreeNode b=new DefaultMutableTreeNode("洛阳"); root.add(a); root.add(b); DefaultMutableTreeNode a1=new DefaultMutableTreeNode("金水区"); a.add(a1); JTree tree =new JTree(root); // 设置tree为单选 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); // 新增 JButton addBtn =new JButton("add"); addBtn.setSize(50,40); addBtn.setLocation(210, 10); // 为tree添加一个选择监听器,当每次选择的时候会获取这个选择的根节点以及子节点对象 tree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { path=e.getPath(); System.out.println(path); } }); addBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 获取被选中的节点 DefaultMutableTreeNode temp=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(temp ==null) { System.out.println("请选择一个节点"); }else { temp.add(new DefaultMutableTreeNode("aaaa")); // 重新展开当前选中的节点 tree.expandPath(path); // 刷新树 tree.updateUI(); } } }); // 删除 JButton delBtn =new JButton("del"); delBtn.setSize(50,40); delBtn.setLocation(300, 10); delBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 获取被选中的节点 DefaultMutableTreeNode temp=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(temp ==null) { System.out.println("请选择一个节点"); } if(temp.isRoot()) { System.out.println("根节点不能删除"); } // 删除当前选中的节点 temp.removeFromParent(); // 重新展开当前选中的节点 tree.expandPath(path); // 刷新树 tree.updateUI(); } }); JScrollPane scrollPane =new JScrollPane(tree); scrollPane.setSize(200,400); scrollPane.setLocation(10, 10); jPanel.add(scrollPane); jPanel.add(addBtn); jPanel.add(delBtn); jFrame.setContentPane(jPanel); // 设置窗体可见 jFrame.setVisible(true); } }
标签:JTree,tree,new,DefaultMutableTreeNode,swing,import,JavaSwing,javax From: https://www.cnblogs.com/liangqingyun/p/18596037