翻译:java中怎样使用树结构(How to Use Trees)

Java JTree的应用与使用方法
本文作者对Java 2 JDK的JTree应用进行翻译。介绍了使用JTree类展示层次数据,包括树结构的节点概念,如根节点、分支节点和叶节点等。还讨论了增加树结构、响应节点选择事件、个性化显示、动态改变树结构等内容,并给出了TreeDemo的代码示例。

     最近闲的慌,听说思维迟缓是衰老的标志,.为了尽量保持年轻,我对java 2 jdk的JTree的应用做了一个翻译,首次尝试翻译,有许多不对的地方,还请高手指正.

        怎样使用树结构(How to Use Trees)(一)

       使用Jtree类,你可以展示有层次关系的数据.一个JTree的实例并不包含你的数据,它只是简单的展示一下你的数据而已.象其他的Swing组件一样,树通过查询它的数据模型得到数据,下面是关于树结构的图片:

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"></shapetype><stroke joinstyle="miter"></stroke><formulas></formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f><lock aspectratio="t" v:ext="edit"></lock><shape id="_x0000_i1025" style="WIDTH: 129pt; HEIGHT: 114pt" alt="A tree" type="#_x0000_t75"></shape><imagedata o:href="http://java.sun.com/docs/books/tutorial/figures/uiswing/components/tree.gif" src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.gif"></imagedata>

         如前所述,JTree垂直的展示它的数据.每一行展示一条树结构的数据,被称作一个”节点”每个树结构都有一个根节点,所有其它节点都由此派生而来.默认的,树结构将显示根节点,但是你也可以做另外的处理.一个节点可以拥有子节点,也可以没有子节点.我们提到的有子节点的节点,叫做分支节点.没有子节点的节点叫做叶节点(leaf nodes).分支节点可以拥有任意多的子节点.用户可以通过点击分支节点来折叠或者展开它----使它的子节点可见或者不可见.默认的,除开根节点的所有分支节点的初始状态都是折叠的.通过监听树结构的扩展事件,程序可以发现分支节点的扩展状态是否改变.

本文余下部分将讨论如下主题:

增加一个树结构

响应节点选择事件

个性花树结构的显示

动态改变树结构

增加一个数字模型

Tree API

使用树的一些例子

 

 

 

增加一个树结构

下面是一个应用的图片,上半部分在一个可滚动面板中展示了一个树结构

<shape id="_x0000_i1026" style="WIDTH: 383.25pt; HEIGHT: 195.75pt" alt="TreeDemo" type="#_x0000_t75"></shape><imagedata o:href="http://java.sun.com/docs/books/tutorial/figures/uiswing/components/TreeDemo.gif" src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image003.gif"></imagedata>

1:用Java Web Start运行TreeDemo.或者自己动手编译如下代码:

文件名称:TreeDemo.java

/**

 * A 1.4 application that requires the following additional files:

 *   TreeDemoHelp.html

 *    arnold.html

 *    bloch.html

 *    chan.html

 *    jls.html

 *    swingtutorial.html

 *    tutorial.html

 *    tutorialcont.html

 *    vm.html

 */

import javax.swing.JEditorPane;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JSplitPane;

import javax.swing.UIManager;

 

 

 

import javax.swing.JTree;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreeSelectionModel;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

 

 

 

import java.net.URL;

import java.io.IOException;

import java.awt.Dimension;

import java.awt.GridLayout;

 

 

 

public class TreeDemo extends JPanel

                      implements TreeSelectionListener {

    private JEditorPane htmlPane;

    private JTree tree;

    private URL helpURL;

    private static boolean DEBUG = false;

 

 

 

    //Optionally play with line styles.  Possible values are

    //"Angled" (the default), "Horizontal", and "None".

    private static boolean playWithLineStyle = false;

    private static String lineStyle = "Horizontal";

   

    //Optionally set the look and feel.

    private static boolean useSystemLookAndFeel = false;

 

 

 

    public TreeDemo() {

        super(new GridLayout(1,0));

 

 

 

        //Create the nodes.

        DefaultMutableTreeNode top =

            new DefaultMutableTreeNode("The Java Series");

        createNodes(top);

 

 

 

        //Create a tree that allows one selection at a time.

        tree = new JTree(top);

        tree.getSelectionModel().setSelectionMode

                (TreeSelectionModel.SINGLE_TREE_SELECTION);

 

 

 

        //Listen for when the selection changes.

        tree.addTreeSelectionListener(this);

 

 

 

        if (playWithLineStyle) {

            System.out.println("line style = " + lineStyle);

            tree.putClientProperty("JTree.lineStyle", lineStyle);

        }

 

 

 

        //Create the scroll pane and add the tree to it.

        JScrollPane treeView = new JScrollPane(tree);

 

 

 

        //Create the HTML viewing pane.

        htmlPane = new JEditorPane();

        htmlPane.setEditable(false);

        initHelp();

        JScrollPane htmlView = new JScrollPane(htmlPane);

 

 

 

        //Add the scroll panes to a split pane.

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

        splitPane.setTopComponent(treeView);

        splitPane.setBottomComponent(htmlView);

 

 

 

        Dimension minimumSize = new Dimension(100, 50);

        htmlView.setMinimumSize(minimumSize);

        treeView.setMinimumSize(minimumSize);

        splitPane.setDividerLocation(100); //XXX: ignored in some releases

                                           //of Swing. bug 4101306

        //workaround for bug 4101306:

        //treeView.setPreferredSize(new Dimension(100, 100));

 

 

 

        splitPane.setPreferredSize(new Dimension(500, 300));

 

 

 

        //Add the split pane to this panel.

        add(splitPane);

    }

 

 

 

    /** Required by TreeSelectionListener interface. */

    public void valueChanged(TreeSelectionEvent e) {

        DefaultMutableTreeNode node = (DefaultMutableTreeNode)

                           tree.getLastSelectedPathComponent();

 

 

 

        if (node == null) return;

 

 

 

        Object nodeInfo = node.getUserObject();

        if (node.isLeaf()) {

            BookInfo book = (BookInfo)nodeInfo;

            displayURL(book.bookURL);

            if (DEBUG) {

                System.out.print(book.bookURL + ":  \n    ");

            }

        } else {

            displayURL(helpURL);

        }

        if (DEBUG) {

            System.out.println(nodeInfo.toString());

        }

    }

 

 

 

    private class BookInfo {

        public String bookName;

        public URL bookURL;

 

 

 

        public BookInfo(String book, String filename) {

            bookName = book;

            bookURL = TreeDemo.class.getResource(filename);

            if (bookURL == null) {

                System.err.println("Couldn't find file: "

                                   + filename);

            }

        }

 

 

 

        public String toString() {

            return bookName;

        }

    }

 

 

 

    private void initHelp() {

        String s = "TreeDemoHelp.html";

        helpURL = TreeDemo.class.getResource(s);

        if (helpURL == null) {

            System.err.println("Couldn't open help file: " + s);

        } else if (DEBUG) {

            System.out.println("Help URL is " + helpURL);

        }

 

 

 

        displayURL(helpURL);

    }

 

 

 

    private void displayURL(URL url) {

        try {

            if (url != null) {

                htmlPane.setPage(url);

            } else { //null url

      htmlPane.setText("File Not Found");

                if (DEBUG) {

                    System.out.println("Attempted to display a null URL.");

                }

            }

        } catch (IOException e) {

            System.err.println("Attempted to read a bad URL: " + url);

        }

    }

 

 

 

    private void createNodes(DefaultMutableTreeNode top) {

        DefaultMutableTreeNode category = null;

        DefaultMutableTreeNode book = null;

 

 

 

        category = new DefaultMutableTreeNode("Books for Java Programmers");

        top.add(category);

 

 

 

        //original Tutorial

        book = new DefaultMutableTreeNode(new BookInfo

            ("The Java Tutorial: A Short Course on the Basics",

            "tutorial.html"));

        category.add(book);

 

 

 

        //Tutorial Continued

        book = new DefaultMutableTreeNode(new BookInfo

            ("The Java Tutorial Continued: The Rest of the JDK",

            "tutorialcont.html"));

        category.add(book);

 

 

 

        //JFC Swing Tutorial

        book = new DefaultMutableTreeNode(new BookInfo

            ("The JFC Swing Tutorial: A Guide to Constructing GUIs",

<
use_sim_time: True amcl: ros__parameters: use_sim_time: ${use_sim_time} # 继承全局时间配置 alpha1: 0.2 alpha2: 0.2 alpha3: 0.2 alpha4: 0.2 alpha5: 0.2 base_frame_id: "base_link" beam_skip_distance: 0.5 beam_skip_error_threshold: 0.9 beam_skip_threshold: 0.3 do_beamskip: False global_frame_id: "map" lambda_short: 0.1 laser_likelihood_max_dist: 2.0 laser_max_range: 100.0 laser_min_range: -1.0 laser_model_type: "likelihood_field" max_beams: 60 max_particles: 2000 min_particles: 500 odom_frame_id: "odom" pf_err: 0.05 pf_z: 0.99 recovery_alpha_fast: 0.0 recovery_alpha_slow: 0.0 resample_interval: 1 robot_model_type: "differential" # 根据机器人类型调整(差速/全向) save_pose_rate: 0.5 sigma_hit: 0.2 tf_broadcast: True transform_tolerance: 0.5 update_min_a: 0.2 update_min_d: 0.25 z_hit: 0.5 z_max: 0.05 z_rand: 0.5 z_short: 0.05 # 行为树导航器配置 bt_navigator: ros__parameters: use_sim_time: ${use_sim_time} global_frame: map robot_base_frame: base_link odom_topic: /odom # 使用绝对路径确保找到正确的BT文件,避免路径冲突 default_bt_xml_filename: "/opt/ros/humble/share/nav2_bt_navigator/behavior_trees/navigate_to_pose_w_replanning.xml" plugin_lib_names: - nav2_compute_path_to_pose_action_bt_node - nav2_compute_path_through_poses_action_bt_node # 添加路径贯穿多个目标点的插件 - nav2_follow_path_action_bt_node - nav2_back_up_action_bt_node - nav2_spin_action_bt_node - nav2_wait_action_bt_node - nav2_clear_costmap_service_bt_node - nav2_is_stuck_condition_bt_node - nav2_goal_reached_condition_bt_node - nav2_goal_updated_condition_bt_node - nav2_initial_pose_received_condition_bt_node - nav2_reinitialize_global_localization_service_bt_node - nav2_rate_controller_bt_node - nav2_distance_controller_bt_node - nav2_speed_controller_bt_node - nav2_truncate_path_action_bt_node - nav2_goal_updater_node_bt_node - nav2_recovery_node_bt_node - nav2_pipeline_sequence_bt_node - nav2_round_robin_node_bt_node - nav2_transform_available_condition_bt_node - nav2_time_expired_condition_bt_node - nav2_path_expiring_timer_condition - nav2_distance_traveled_condition_bt_node - nav2_remove_passed_goals_action_bt_node # 控制器配置(本地规划) controller_server: ros__parameters: use_sim_time: ${use_sim_time} controller_frequency: 20.0 min_x_velocity_threshold: 0.001 min_y_velocity_threshold: 0.5 # 差速机器人可设为0.0 min_theta_velocity_threshold: 0.001 failure_tolerance: 0.3 progress_checker_plugin: "progress_checker" goal_checker_plugins: ["general_goal_checker"] controller_plugins: ["FollowPath"] # 进度检查器参数 progress_checker: plugin: "nav2_controller::SimpleProgressChecker" required_movement_radius: 0.5 movement_time_allowance: 10.0 # 超时未移动则触发恢复行为 # 目标检查器参数 general_goal_checker: plugin: "nav2_controller::SimpleGoalChecker" xy_goal_tolerance: 0.25 # 位置误差容忍度(米) yaw_goal_tolerance: 0.25 # 角度误差容忍度(弧度) stateful: True # DWB本地规划器参数 FollowPath: plugin: "dwb_core::DWBLocalPlanner" debug_trajectory_details: True min_vel_x: 0.0 max_vel_x: 0.26 # 最大线速度(根据机器人调整) min_vel_y: 0.0 max_vel_y: 0.0 # 差速机器人Y方向速度为0 min_vel_theta: -1.82 max_vel_theta: 1.82 acc_lim_x: 2.5 acc_lim_y: 0.0 acc_lim_theta: 3.2 decel_lim_x: -2.5 decel_lim_y: 0.0 decel_lim_theta: -3.2 vx_samples: 20 vy_samples: 1 # 差速机器人Y方向采样数设为1 vtheta_samples: 20 sim_time: 1.7 linear_granularity: 0.05 angular_granularity: 0.025 transform_tolerance: 0.5 xy_goal_tolerance: 0.25 yaw_goal_tolerance: 0.25 trans_stopped_velocity: 0.25 rot_stopped_velocity: 0.25 prune_plan: True critics: ["RotateToGoal", "Oscillation", "BaseObstacle", "GoalAlign", "PathAlign", "PathDist", "GoalDist"] BaseObstacle.scale: 0.02 PathAlign.scale: 32.0 PathAlign.forward_point_distance: 0.1 GoalAlign.scale: 24.0 GoalAlign.forward_point_distance: 0.1 PathDist.scale: 32.0 GoalDist.scale: 24.0 RotateToGoal.scale: 32.0 RotateToGoal.slowing_factor: 5.0 RotateToGoal.lookahead_time: -1.0 # 局部代价地图配置 local_costmap: local_costmap: ros__parameters: use_sim_time: ${use_sim_time} update_frequency: 5.0 # 更新频率(Hz) publish_frequency: 2.0 # 发布频率(Hz) global_frame: odom # 局部地图参考系(里程计) robot_base_frame: base_link rolling_window: true # 滚动窗口(跟随机器人移动) width: 3 # 宽度(米) height: 3 # 高度(米) resolution: 0.05 # 分辨率(米/格) transform_tolerance: 0.5 plugins: ["voxel_layer", "inflation_layer"] voxel_layer: plugin: "nav2_costmap_2d::VoxelLayer" enabled: True publish_voxel_map: True origin_z: 0.0 z_resolution: 0.05 z_voxels: 16 max_obstacle_height: 2.0 mark_threshold: 0 observation_sources: scan # 激光雷达数据来源 scan: topic: /scan # 激光雷达话题(确保与实际一致) max_obstacle_height: 2.0 clearing: True # 允许清除障碍物 marking: True # 允许标记障碍物 data_type: "LaserScan" raytrace_max_range: 3.0 raytrace_min_range: 0.0 obstacle_max_range: 2.5 obstacle_min_range: 0.0 inflation_layer: plugin: "nav2_costmap_2d::InflationLayer" enabled: True inflation_radius: 0.55 # 膨胀半径(根据机器人半径调整) cost_scaling_factor: 10.0 inflate_unknown: False always_send_full_costmap: True # 全局代价地图配置 global_costmap: global_costmap: ros__parameters: use_sim_time: ${use_sim_time} update_frequency: 1.0 publish_frequency: 1.0 global_frame: map # 全局地图参考系(地图) robot_base_frame: base_link robot_radius: 0.22 # 机器人半径(米,根据实际调整) resolution: 0.05 transform_tolerance: 0.2 plugins: ["static_layer", "obstacle_layer", "inflation_layer"] static_layer: plugin: "nav2_costmap_2d::StaticLayer" enabled: True map_subscribe_transient_local: True obstacle_layer: plugin: "nav2_costmap_2d::ObstacleLayer" enabled: True observation_sources: scan scan: topic: /scan max_obstacle_height: 2.0 clearing: True marking: True data_type: "LaserScan" raytrace_max_range: 3.0 raytrace_min_range: 0.0 obstacle_max_range: 2.5 obstacle_min_range: 0.0 inflation_layer: plugin: "nav2_costmap_2d::InflationLayer" enabled: True inflation_radius: 0.55 cost_scaling_factor: 10.0 inflate_unknown: False always_send_full_costmap: True # 地图服务器配置 map_server: ros__parameters: use_sim_time: ${use_sim_time} # 确保地图文件路径正确(.yaml和.pgm文件需在同一目录) yaml_filename: "/home/yahboom/hex_aqacs_planner/map.yaml" # 规划器服务器配置(全局规划) planner_server: ros__parameters: use_sim_time: ${use_sim_time} expected_planner_frequency: 20.0 planner_plugins: ["AQACSPlanner"] # 使用AQACS规划器 AQACSPlanner: # 修正插件名称格式,使用命名空间形式 plugin: "hex_aqacs_planner::AQACSPlanner" alpha: 1.0 beta: 2.0 rho: 0.1 ant_count: 50 q: 100.0 hex_side: 0.1 # 六边形边长(米) # 生命周期管理器配置(关键:管理节点启动顺序) lifecycle_manager_navigation: ros__parameters: use_sim_time: ${use_sim_time} # 节点启动顺序:先启动地图和定位,再启动规划和控制 node_names: ["map_server", "amcl", "planner_server", "controller_server", "bt_navigator"] autostart: True # 自动启动所有节点 bond_timeout: 10000 # 超时时间(毫秒) 帮我修改一下吧
最新发布
10-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值