简体   繁体   English

Java Swing:如何正确实例化GUI并传递域对象?

[英]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. 更复杂的子元素(如JListJTree )已经具有关联的数据模型。 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.

相关问题 如何在Java Swing中创建以下GUI? - How do I create the following GUI in Java Swing? 如何在Java中使域对象适应GUI - How to adapt domain objects to GUI in Java 如何在此Java Swing代码中将GUI与逻辑正确分开 - How to properly separate GUI from the Logic in this Java Swing code 如何在Java RMI中使用可序列化对象和回调方法正确模仿传递引用? - How do I properly mimic pass-by-reference in Java RMI with serializable objects and a callback method? 如何让我的 Java GUI 正确计算字段? - How do I get my Java GUI to calculate fields properly? 如何在Java swing中以线程安全的方式初始化gui对象? - How to initialize gui objects in a thread safe manner in java swing? 滑块在Java Swing GUI中无法正常工作 - Slider not working properly in Java Swing GUI 如何使用drawString和drawImage为游戏实现Java swing GUI启动屏幕? - How do I implement Java swing GUI start screen for a game with drawString and drawImage? 如何将我的sqlite数据库连接到我的java swing GUI? - How do i connect my sqlite db to my java swing GUI? 使用BoxLayout面板构造Java GUI时,如何在同一行上添加放置两个swing组件? - How do I add put two swing components on the same line when constructing a Java GUI with BoxLayout panels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM