如何打开Java Deque< T>进入DefaultListModel? [英] How do I turn a Java Deque<T> into a DefaultListModel?

查看:103
本文介绍了如何打开Java Deque< T>进入DefaultListModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个包含Deque<T>的类(我们称它为Model.java),其中包含用于使项目入队和出队的方法.现在,我试图将其绑定到GUI JList.我对如何以某种方式使用我的模型"数据(即Deque)作为JList想要的DefaultListModel感到困惑.我仍然在努力获得面向对象的概念,因为它们适用于GUI编程. DefaultListModel文档状态:

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:

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

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 ....

是否有某种方法可以使DefaultListModel使用我的Deque<T>而不是Vector,从而允许我的Model.java代码在很大程度上保持不变,同时免费提供所有侦听/通知行为?还是我必须重写Model.java以使用DefaultListModel代替Deque<T>?

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>?

推荐答案

请注意,JList构造函数使用ListModel(接口),而不使用DefaultListModel(实现).这是OO原则(合同),指定JList可以使用碰巧实现ListModel接口的任何对象.从面向对象的编程概念上的Java教程:

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.

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

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
{
     ....

,并将包含实现ListModel方法的四个其他方法.这些实现可以根据需要执行任何操作,但必须遵循ListModel的定义以及JavaDoc中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.

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

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

这篇关于如何打开Java Deque&lt; T&gt;进入DefaultListModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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