如何将 Java Deque转换为 Java Deque进入默认列表模型?

发布于 2024-11-25 07:05:26 字数 577 浏览 4 评论 0原文

我编写了一个类(我们称之为 Model.java),其中包含一个 Deque,以及用于将项目入队和出队的方法。现在我试图将其与 GUI JList 联系起来。我对如何以某种方式使用我的“模型”数据(Deque)作为 JList 想要的 DefaultListModel 感到困惑。我仍在努力真正理解面向对象的概念,因为它们适用于 GUI 编程。 DefaultListModel 文档指出:

此类松散地实现了 java.util.Vector API,因为它实现了 java.util.Vector 的 1.1.x 版本,没有集合类支持,并且在发生更改时通知 ListDataListener。目前它委托给一个 Vector ....

有什么方法可以让 DefaultListModel 使用我的 Deque 而不是 Vector,从而允许我的 Model.java 代码在提供时保持基本不变所有的监听/通知行为都是免费的吗?或者我是否必须重写 Model.java 以使用 DefaultListModel 而不是 Deque

I wrote a class (let's call it Model.java) that contains a Deque<T>, with methods for enqueuing and dequeuing items. Now I'm trying to tie this to a GUI JList. I'm baffled by how to somehow use my "model" data -- the Deque -- as a DefaultListModel that JList wants. I'm still struggling to really get OO concepts, as they apply to GUI programming. DefaultListModel documentation states:

This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support, and notifies the ListDataListeners when changes occur. Presently it delegates to a Vector ....

Is there some way to get the DefaultListModel to use my Deque<T> instead of a Vector, thus allowing my Model.java code to remain largely unchanged while providing all the listening/notifying behavior for free? Or do I have to rewrite Model.java to use a DefaultListModel instead of Deque<T>?

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

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

发布评论

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

评论(3

注定孤独终老 2024-12-02 07:05:26

请注意,JList 构造函数采用 ListModel(接口),而不是 DefaultListModel(实现)。这是一个 OO 原则(契约),指定 JList 可以使用恰好实现 ListModel 接口的任何对象。来自面向对象编程概念的 Java 教程:

接口是类与外部世界之间的契约。什么时候
类实现接口,它承诺提供行为
由该接口发布。

由于 ListModel 只有四个方法,因此您的类应该很容易实现它们并将操作委托给内部 Deque。您的类应声明为

public class Model implements ListModel
{
     ....

并将包含四个实现 ListModel 方法的附加方法。这些实现可以在幕后执行您需要的任何操作,但必须遵守 ListModel 的定义以及 JavaDoc 中作为 ListModel 契约的一部分指定的任何行为。

完成此操作后,您可以构造一个 JList,将类 Model 的实例传递给构造函数。

Notice that the JList constructor takes a ListModel (an interface), not a DefaultListModel (an implementation). This is an OO principle (Contract) specifying that JList can use ANY object that happens to implement the ListModel interface. From the Java tutorial on Object Oriented Programming Concepts:

An interface is a contract between a class and the outside world. When
a class implements an interface, it promises to provide the behavior
published by that interface.

Since ListModel has only four methods, it should be very easy for your class to implement them and delegate the operations to your internal Deque. Your class should be declared as

public class Model implements ListModel
{
     ....

and will contain four additional methods that implement the methods of ListModel. The implementations can do whatever you need under the covers, but must adhere to the definition of ListModel and whatever behavior is specified as part of the ListModel contract, in the JavaDoc.

Once you have done this, you can construct a JList passing an instance of your class Model to the constructor.

伤痕我心 2024-12-02 07:05:26

对于JList,您不必必须使用DefaultListModel,只需使用ListModel接口的一些实现即可。使用Deque 可以很容易地实现后者。

For JList, you don't have to use DefaultListModel, just some implementation of the ListModel interface. And the latter is very achievable using a Deque.

酒中人 2024-12-02 07:05:26

我不知道如何处理 addListDataListener()

AbstractListModel 可能是一个很好的起点,因为它已经实现了规定的 EventListenerList 方法处理侦听器和事件。

I didn't know what to do for addListDataListener()

AbstractListModel might be a good starting point, as it already implements the prescribed EventListenerList methods to deal with listeners and events.

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