GWT-EXT - 事件发生后将小部件添加到特定 ContentPanel 的最佳方式是什么?
第一篇文章不要伤害我:)
我正在使用带有通常的北、西、中、南面板的 BorderLayout
。 在西 ContentPanel
上,我有一棵树
。 如果发生事件 (OnClick
),我希望在中心 ContentPanel
上显示一个特定的对话框。
对我来说最好的方法是什么? 目前,我正在使用一个名为 returnPanel()
的函数,该函数返回中心 ContentPanel
。 在事件处理程序中,我调用此函数(MainWindow.returnPanel().add(myDialog))
。
first post don't hurt me :)
I am using a BorderLayout
with the usual North, West, Center, South Panels. On the West ContentPanel
, I've got a Tree
. If an event (OnClick
)occurs I want a particular dialog box displayed on the Center ContentPanel
.
What is the best way for me to do this? Currently I'm using a function called returnPanel()
that returns the center ContentPanel
. In the event handler I call this function (MainWindow.returnPanel().add(myDialog))
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您这样做的方式很直观并且有效,但是当应用程序增长时就会开始造成地狱,因为应用程序的不同部分是强耦合的。 这个问题的解决方案是 MVC 设计模式 和 观察者设计模式。
理想情况下,使用 MVC 模式,您不希望任何小部件“了解”任何其他小部件。 只有一个类知道所有的小部件,那就是控制器。 每当一个小部件需要向另一个小部件发送消息/信号时,它都会将其告知 Controller 类,后者以适当的方式将消息中继到适当的小部件。 这样,两个小部件就解耦了,一个可以在不破坏另一个的情况下进行更改。 您可能希望使用枚举来枚举控制器必须响应的所有可能操作。
如果您的小部件在事件发生时只需要调用控制器,您可以简单地调用一个适当命名的(静态)方法并完成它。 但是,一旦需要向多个其他类通知某个事件,您最好使用观察者模式,它允许您向多个其他类发出信号,而无需更改您的类。 它只是在 eventHandler 中调用 notificationPObservers() 即可。 有多少听众以及他们是什么类型都无关紧要。 这样,您还可以将类与其侦听器解耦。 即使只有控制器监听,也可能建议使用该模式,因为它清楚地将“回调”代码与类中的其他代码分开。
顺便说一句,这与 GWT 甚至 Java 无关。
The way you are doing it is intuitive and works, but will start causing hell when the application grows, because different parts of the application are strongly coupled. The solutions to this problems are the MVC design pattern and the observer design pattern.
Ideally, using the MVC pattern, you don't want any widget to 'know' of any other widget. There is only class that knows all the widgets, which is the Controller. Anytime one widget needs to message/signal another widget, it tells it to the Controller class, which relays the message in the appropriate way to the appropriate widget. In this way, the two widgets are decpoupled and one can change without breaking the other. You may want to use an enum to enumerate all possible actions to which the controller has to responsd.
If your widget has to call only the Controller when an event occurs, you may simply call an aptly named (static) method on it and be done with it. However, as soon as multiple other classes needs to be informed of an event, you are better of using the Observer pattern, which allows you to signal multiple other classes, without changing your class. It simply calls notifyPObservers() in the eventHandler and that's it. How many listeners there are, and what type they are, is irrelevant. This way, you also decouple a class from it's listeners. Even if only the Controller listens, it may be advisable to use the pattern, as it clearly seperated the 'call back' code from the other code in the classes.
BTW, this has nothing to do with GWT or even Java in particular.