Java Swing:列表模型和集合

发布于 2024-11-19 22:09:28 字数 526 浏览 1 评论 0原文

我目前正在编写一个程序,该程序在 GUI 方面和数据存储方面之间存在大量代码重复,我想知道我提出的解决方案是否遵循可接受的设计原则。

基本上,我有一些数据对象,其中有一个包含更多信息的列表。然后 GUI 显示该对象,以及一个 JList,该 JList 显示数据对象列表中的所有信息。在 GUI 中,用户可以向对象列表添加或删除信息,这会直观地反映在 JList 中。

然而,这导致我必须分别维护 GUI JList 和对象列表:(

private void addInformation(String s) {
    this.listmodel.addElement(s);
    this.dataObject.getList().add(s);
}

显然这是一个非常简单的示例,但您明白了)。这也意味着我使用了两倍的内存来保存这两个列表。我可以接受创建一个同时实现 ListModel 和 List 接口的类,然后只需要更新单个组合列表/列表模型吗?我还在大学学习,我想让这个项目在其他人的可维护性和遵守最佳实践方面尽我所能。

I am currently writing a program which is having a lot of code duplication between the GUI aspect and the data storage aspect, and I was wondering if my proposed solution follows acceptable design principles.

Basically, I have some Data objects, which have a List holding some more information. Then the GUI displays the object, and a JList which displays all the information in the Data object's List. In the GUI, users can add or remove information to the object's list, and this is visually reflected in the JList.

However, this results in me having to maintain the GUI JList and the object List separately:

private void addInformation(String s) {
    this.listmodel.addElement(s);
    this.dataObject.getList().add(s);
}

(Obviously that is a very simple example but you get the idea). It also means that I'm using up twice as much memory holding both lists. Would it be acceptable for me to create a class which implemented both the ListModel and the List interfaces and then only need to update the single combined list/listmodel? I'm still studying at uni and I want to make this project the best I possibly can in terms of maintainability by others and adherence to best practices.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我也只是我 2024-11-26 22:09:28

您的 GUI 不应包含有关数据模型状态的信息。最好将其分离为模型-视图-控制器 (MVC) 设计。控制器负责对象创建并相应地更新模型和视图。您的实际视图应该只负责通过向其传递参数来显示屏幕显示。这样,视图和模型不需要彼此了解任何信息,并且只有一个数据模型。

如果您必须保持两个列表一致,那么您的设计就有问题,并且可能会在代码中引入更多错误。

请参阅此处了解 MVC 详细信息。

Your GUI should not hold information about the state of the data model. It would be better to separate it up into a Model-View-Controller (MVC) design. Where the controller is responsible for object creation and updating the model and view accordingly. Your actual view should just be responsible for screen display by passing arguments to it to display. This way the view and model don't need to know anything about each other and there will only be one data model.

If your having to keep both lists consistent then something is wrong with your design and will probably introduce more bugs into the code.

See here for MVC details.

ま柒月 2024-11-26 22:09:28

我建议您使用 ListModel 接口的实现(例如 DefaultListModel)来保存您的数据。然后 绑定它 到您的 JList。如果默认渲染器不适合,请编写您自己的自定义 ListCellRenderer 和 将其应用于 JList 对象。

I'd suggest you use an implementation of the ListModel interface (such as DefaultListModel) to hold your data. Then bind it to your JList. If the default renderer doesn't suit, write your own customized ListCellRenderer and apply it to the JList object.

红ご颜醉 2024-11-26 22:09:28

Java Swing 使用可分离的模型架构来最小化模型之间的耦合(例如ListModelTable Model)和相应的视图(JListJTable)。您不必使用更多内存。

Java Swing use a separable model architecture to minimize coupling between the model (e.g. ListModel, Table Model) and the corresponding view (JList, JTable). You don't have to use more memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文