树模型创建 [英] TreeModel creation

查看:31
本文介绍了树模型创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 中的树模型有一些疑问.

I have some questions regarding a treemodel in Java.

在过去 13 周的课堂上,我们一直在开发联系人管理器.此联系人管理器具有各种组件:联系人可以是业务联系人或个人联系人,并且每个组件都有自己的一组事件.他们还有地址、社交网络和电话号码.

For the last 13 weeks in class we've been developing a contact manager. This contact manager has various components: a contact can either be a business contact or personal contact, and each has their own set of events. They also have addresses, social networks, and phone numbers.

上一个作业,我们必须从外部 txt 文件中读取信息并在我们的项目中实现它.那很顺利.现在他想让我们根据上述文本中的信息构建一个 treeModel,但我很难理解这个树模型是如何工作的.

Last assignment we had to read information from an outside txt file and implement it in our project. That went fine. Now he wants us to build a treeModel out of the information from said text, but I'm having a really hard time understanding how this tree model works.

例如,root、parent、children、nodes 都是我试图定义但很难定义的词.在这种情况下,我的根是我的联系人经理,我的父母是联系人,孩子是地址之类的吗?如果有人能帮助我理解它,那真的很有帮助.

For example, root, parent, children, nodes are all words I'm trying to define but having a really hard time. In this case would my root be my contact manager, my parent by a contact and children be like address and stuff? If someone could help me understand it it would really help.

这是作业的摘录,如果它有助于您更多地理解我的问题:

Here's a excerpt from the assignment if it helps you understand my question more:

  • 创建扩展 JFrame 的类 ContactManagerFrame.它的构造函数将一个 ContactManager 对象(从保存在磁盘上的序列化文件中检索)作为参数.

  • Create class ContactManagerFrame that extends JFrame. Its constructor will take as argument a ContactManager object (retrieved from the serialized file saved on disk).

创建和布局所需的图形组件.(提示:使用JPanel对过滤功能的组件进行分组,将JTree添加到JScrollPane,添加组件时使用BorderLayout)

Create and layout the required graphical components. (Hint: use a JPanel to group the components of the filtering feature, add the JTree to a JScrollPane, use BorderLayout when adding the components)

Create 方法 createNodes() 将对应于树根(即Contacts"节点)的 DefaultMutableTreeNode 和对应于用户指定的过滤器的 String 作为参数.此方法应为联系人和每个联系人引用的对象创建节点,并将它们组织成树状结构,如图 1 所示.您应避免创建空父节点(例如,如果联系人没有任何地址,您不应为此特定联系人添加父节点地址").如果指定的过滤器是空字符串,则 createNodes() 将包括存储在 ContactManager 对象中的所有联系人.在任何其他情况下,createNodes() 将包含 ContactManager 中方法 searchContacts() 返回的联系人.

Create method createNodes() that takes as arguments a DefaultMutableTreeNode corresponding to the root of the tree (i.e., "Contacts" node) and a String corresponding to the filter specified by the user. This method should create nodes for the contacts and the objects referenced by each contact and organize them in a tree structure, as shown in Figure 1. You should avoid the creation of empty parent nodes (e.g., if a contact does not have any addresses, you should not add a parent node "Addresses" for this particular contact). If the specified filter is an empty string, createNodes() will include all contacts stored in the ContactManager object. In any other case, createNodes() will include the contacts returned by method searchContacts() in ContactManager.

从根节点创建一个 DefaultTreeModel 对象.使用 DefaultTreeModel 初始化 JTree 组件.'

Create a DefaultTreeModel object from the root node. Use the DefaultTreeModel to initialize the JTree component.'

我不会要求你们中的任何人为我或任何事情解决它.我只是真的想了解它.非常感谢!

I'm not asking any of you to solve it for me or anything. I just really want to understand it. Thank you very much!

推荐答案

更复杂的 Swing 组件,例如 JTree, JTable, JListJComboBox模型的概念.它的意思是:组件正在显示的下层数据.它们以这种方式设计,将数据本身与其视觉"表示(又名视图)分开,并允许开发人员忘记"(或多或少)关于数据表示.因此,如教程中所述,如果您需要添加新数据要在这些组件之一上显示的对象,您只需将其添加到模型视图就会自动更新.

More sophisticated Swing components such as JTree, JTable, JList or JComboBox work with the concept of model. It means: the subjacent data that is being displayed by the component. They are designed in this way to have separate the data itself from their "visual" representation (a.k.a. the view) and allow the developer "forgot" (more or less) about data representation. So as explained in the tutorials, if you need to add a new data object to be displayed on one of these components you only have to add it to the model and the view will be automatically updated.

说到这里你会看到这些组件有一个以模型作为参数的构造函数:

Having said this you'll see these components have a constructor that takes a model as argument:

这些模型由接口定义,这些接口建立了任何具体实现都必须满足的基本契约.

These models are defined by interfaces which establish the basic contract that any concrete implementation must meet.

特别是在 JTree 的案例中,我们有 TreeModel 接口和默认实现:默认树模型.此外,任何 TreeModel 都必须使用节点对象,这些节点对象必须实现 TreeNode 接口.

Particulalry in JTree's case we have the TreeModel interface and a default implementation: DefaultTreeModel. Additionaly any TreeModel has to work with node objects which have to implement the TreeNode interface.

因此,要使用 JTree,您需要一个 TreeModel 和一堆通过父子关系相关的 TreeNode.例如这样的事情:

So, to work with JTree you'll need a TreeModel and a bunch of TreeNodes related through a parent-child relationship. For instance something like this:

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Contacts"); // root node

DefaultMutableTreeNode contact1 = new DefaultMutableTreeNode("Contact # 1"); // level 1 node
DefaultMutableTreeNode nickName1 = new DefaultMutableTreeNode("drocktapiff"); // level 2 (leaf) node
contact1.add(nickName1); 

DefaultMutableTreeNode contact2 = new DefaultMutableTreeNode("Contact # 2");
DefaultMutableTreeNode nickName2 = new DefaultMutableTreeNode("dic19");        
contact2.add(nickName2);

root.add(contact1);
root.add(contact2);

DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);

图片

这篇关于树模型创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆