Java Swing:如何正确实例化 GUI 并传递域对象?
我有一个带有嵌套面板的 GUI(带有嵌套面板等选项卡)。我需要将域对象传递给深度嵌套的面板。我可以想到两种方法:
在一个地方实例化所有 gui 对象,例如框架类。那 将使传递域对象变得非常简单,但 Frame 类会 巨大且难以维护。
每个面板都有自己的类,我们在其中实例化并布局它
成分。现在它很容易维护并且类很干净,但是如何
我是否要沿着链传递我的域对象?我不想链式传递 它们通过面板的构造函数,甚至不应该知道它们的
存在。顶层面板上会有大量这些对象 每个面板都有
这两种方式似乎都不是解决方案。您通常如何处理这个问题?
I have a GUI with nested panels(tabbed with nested panels and etc). I need to pass domain object to deeply nested panel. I can think of two ways:
Instantiate all gui objects in one place, like frame class. That
would make passing domain objects dead simple, but Frame class will
be huge and hardly maintanable.Each panel has its own class, where we instantiate and layout its
components. Now its easy to maintain and classes are clean, but how
do I pass down the chain my domain objects? I dont want to chain-pass
them through constructors of panels that shouldn't even know their
existance. And top level panels would have a ton of these objects to
start with.
Niether way seems like a soulution. How do you usually aproach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我组装 Java Swing GUI 时,每个主要 GUI 元素都有一个数据模型。请注意,这不是 MVC 模式。这更像是一种本地MV模式。如果需要,您可以将 GUI 元素侦听器视为“控制器”。
你的想法是正确的,尽管你不应该做太多的传球。
我的
JFrame
(或JApplet
)将具有全局类型字段的关联模型类。该模型类的实例通常会传递给子元素。这是为了允许子元素在选择菜单选项时正确反应(作为示例),我的 JPanel 将具有一个关联的模型类,用于维护文本或按钮子元素的状态。
更复杂的子元素(例如
JList
或JTree
)已经具有关联的数据模型。为了方便起见,我可能会将这些关联的数据模型包装到 JPanel 模型类中。子元素将触发某种选择或动作侦听器。除了与父级关联的模型类之外,其中一些侦听器可能还需要访问模型类。在这种情况下,您必须将模型类的实例传递给侦听器。
When I put together a Java Swing GUI, I have a data model for each major GUI element. Note that this isn't an MVC pattern. This is more like a local MV pattern. If you want, you can consider GUI element listeners as the "controller".
You have the right idea, although you shouldn't have to do much passing.
My
JFrame
(orJApplet
) will have an associated model class of global type fields. An instance of this model class will usually be passed along to the children elements. This is to allow children elements to react properly when a menu option is selected (as an example)My
JPanel(s)
will have an associated model class that maintains the state of text or button children elements.More complicated children elements, like a
JList
or aJTree
, already have an associated data model. I will probably wrap these associated data models into theJPanel
model class for convenience.The children elements will trigger some sort of selection or action listener. Some of these listeners might need access to model classes besides the model class associated with the parent. In this case, you're going to have to pass the instances of your model classes to the listeners.
这是一种责任链模式。我要做的就是创建一个包含所有显示对象的地图,并将其从构造函数传递到构造函数。这样每个实例都可以从地图中获取它需要的东西,而不用关心那里还有什么。
This is sort of a Chain of Responsibility pattern. What I would do is have something that creates a map with all of your display objects in it and pass it from constructor to constructor. That way every instance can take what it needs from the map without caring what else is there.