[英]Java Swing: how do I properly instantiate GUI and pass domain objects?
I have a GUI with nested panels(tabbed with nested panels and etc). 我有一个嵌套面板的GUI(带有嵌套面板的标签等)。 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. 在一个地方实例化所有gui对象,比如帧类。 That would make passing domain objects dead simple, but Frame class will be huge and hardly maintanable. 这将使传递域对象变得简单,但Frame类将是巨大的并且难以管理。
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. Niether的方式似乎是一种洗礼。 How do you usually aproach this? 你通常如何接受这个?
When I put together a Java Swing GUI, I have a data model for each major GUI element. 当我整合Java Swing GUI时,我有一个每个主要GUI元素的数据模型。 Note that this isn't an MVC pattern. 请注意,这不是MVC模式。 This is more like a local MV pattern. 这更像是一个本地MV模式。 If you want, you can consider GUI element listeners as the "controller". 如果需要,可以将GUI元素侦听器视为“控制器”。
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? 现在它易于维护,类很干净,但是如何传递我的域对象链呢?
You have the right idea, although you shouldn't have to do much passing. 你有正确的想法,虽然你不应该做太多的传球。
My JFrame
(or JApplet
) will have an associated model class of global type fields. 我的JFrame
(或JApplet
)将具有关联的全局类型字段的模型类。 An instance of this model class will usually be passed along to the children elements. 此模型类的实例通常会传递给children元素。 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. 我的JPanel(s)
将有一个关联的模型类,它维护文本或按钮子元素的状态。
More complicated children elements, like a JList
or a JTree
, already have an associated data model. 更复杂的子元素(如JList
或JTree
)已经具有关联的数据模型。 I will probably wrap these associated data models into the JPanel
model class for convenience. 为方便起见,我可能会将这些关联的数据模型包装到JPanel
模型类中。
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. 这样每个实例都可以从地图中获取所需的内容,而无需关心其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.