首页 > 编程语言 >JavaSwing JTree

JavaSwing JTree

时间:2024-12-09 21:10:10浏览次数:10  
标签:JTree tree new DefaultMutableTreeNode swing import JavaSwing javax

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

相关文章

  • JavaSwing JCombox
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JList
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JSlider
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JProcessBar
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JRadioButton
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JCheckBox
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JTextArea
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JTextField
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JLabel
    JLabellabel=newJLabel();//设置标签文本label.setText("标签组件");//设置文本水平居中label.setHorizontalTextPosition(SwingConstants.CENTER);//设置文本上下居中label.setVerticalTextPosition(SwingConstants......
  • JavaSwing JButton
    JButtonbtn01=newJButton("btn01");//设置按钮图标//btn01.setIcon(newImageIcon(HelloWorld.class.getResource("/images/book.png")));//设置按钮被按下后图标//btn01.setPressedIcon(newImageIcon(HelloWorld.class.getRes......