MVC Java:控制器如何将侦听器设置为视图的子类 [英] MVC Java: How does a Controller set listeners to the children classes of a View

查看:62
本文介绍了MVC Java:控制器如何将侦听器设置为视图的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,以及一个带有许多子视图的视图,其中有多个孩子. 示例:JPanel中的JPanel具有用于控制器传递到模型的按钮和字段.

I have a Controller, and a View with many children views with children with children. Example: JPanels within JPanels that have buttons and fields for a controller to pass to the model.

我目前的操作方式是在视图中实例化具有动作侦听器的控制器"并访问我的单例模型.这行得通-但绝对不是MVC.

The current way I'm doing it is instantiating 'Controllers' in the view that have action listeners and access my models which are singletons. This works- But it's definitely not MVC.

所以问题是-我该怎么办?

So the question is- how do I do it?

是从控制器进行菊花链连接的唯一方法: mainview.getSubView().getSubView().getSubView().setActionListener(new AL()); 和: mainview.getSubView().getSubView().getSubView().getSomeTextFieldText();

Is the only way to daisy chain from the controller: mainview.getSubView().getSubView().getSubView().setActionListener(new AL()); and: mainview.getSubView().getSubView().getSubView().getSomeTextFieldText();

这似乎是非常不切实际的[我看不到这种方法的好处]

This seems extremely impractical [and I can't see the benefits of this method]

任何提示或指导都将有所帮助.我没有看到的教程可以用子视图来解释MVC.

Any tips or guidance would be helpful. No tutorial I see explains MVC with children views.

顺便说一句-我知道我应该让视图和模型使用可观察的/观察者-但是即使是那些视图和模型,我在设置动作侦听器时还是有问题.

On a side note- I know I should have the views and models use observable/observer - but even with those I have a problem with setting action listeners.

推荐答案

我认为您在概念上将视图"绑定到各个GUI组件时犯了一个错误.在MVC模型中,确实没有子视图"这样的概念.

I think you are making a mistake of conceptually tying a "view" to individual GUI components. In the MVC model there really is not such concept as a "subview".

例如,如果您有一个框架,其中包含许多面板和子面板以及诸如按钮等子组件,则整个事物是MVC中的视图/控制器界面.您的顶级框架(或某些封装类,也许您的GUI有很多框架,没关系)将为控制器(通过事件)和视图(例如侦听器)提供接口.确切地说,您的UI的安排在其后被抽象了.将整个UI视为一个黑匣子.如何从内部组件调度/提供事件是UI实现的一部分,这很可能涉及事件链和委托等.但这不是视图/控制器所关心的.

If you have a frame, for example, with many panels and subpanels and subcomponents such as buttons, etc., that entire thing is the view/controller interface in MVC. Your top-level frame (or some encapsulating class, perhaps your GUI has many frames, it doesn't matter) would provide the interface to the controller (via, say, events) and the view (via, say, listeners). Exactly how your UI is arranged is abstracted behind that. Think of your entire UI as a black box. How events are dispatched / provided from the internal components is part of the UI implementation, and this may very well involve event chains and delegates and etc. -- but that is not something the view/controller is concerned with.

所以您最终得到的是(概念示例):

So you end up with something like (conceptual example):

Model m = new Model();
View v = new View(m);
Controller c = new Controller(m);
MyFrame gui = new MyFrame(v, c);

然后:

public MyFrame (View v, Controller c) {

   // register listeners to view, e.g.
   v.addChangeListener(this /* or some other ui component */);

   // send events to controller, e.g.
   addActionListener(c /* or some interface that c provides */).

   // or even:
   deleteButton.addActionListener(new ActionListener(){
       @Override public void actionPerformed (ActionEvent e) {
           c.doDelete();
       }
   });   

}

在理想情况下,您可以完全重做GUI的层次结构,拥有完全不同的组件树和组织,并且只要通过GUI传达的信息保持不变,视图和控制器也保持不变.

In an ideal situation, you can completely rework the hierarchy of your GUI, having a totally different component tree and organization, and, providing the information being conveyed through the GUI remains unchanged, the view and controller remain unchanged as well.

一旦您习惯了MVC模式,您可能会开始四处走动(例如,在许多情况下,视图和/或控制器只是事件的中间人,而GUI本身有时最终会封装起来)整个控制器和视图概念,为您提供GUI,模型和一堆事件侦听器-Swing事件体系结构本身就是MVC,并且边界可能会变得更加模糊.没关系.但是,即使在控制器/视图只是抽象概念而不是具体类的情况下,视图或控制器也没有理由必须知道GUI中的结构和对象树.

Once you get used to the MVC pattern, you may start to take shortcuts here and there (for example, in many cases the view and/or controller are just middle-men for events, and the GUI itself sometimes ends up encapsulating the entire controller and view concepts, leaving you with a GUI, a model, and a bunch of event listeners -- the Swing event architecture is fairly MVC in itself), and boundaries may get fuzzier. That's OK. But there's no reason that either the view or the controller have to know about the structure and object tree in the GUI -- even in cases where controller/view are just abstract concepts instead of concrete classes.

有时,在使用Swing时,MVC会变得特别模糊,因为您最终会自然地以MVC方式进行所有操作,因此,当您尝试在架构上显式施加MVC模式时,您会想知道为什么这样做没有效果.似乎根本没有太大变化(并且很难看到收益,因为您忘记了自己已经在做).有时,它更多是一种思考的方式,而不是一种具体的这样做的方式.

Sometimes MVC can get especially fuzzy when working with Swing because you kind of end up doing everything in an MVC way naturally, so when you try to explicitly impose an MVC pattern on your architecture, you're left wondering why that doesn't seem to change much at all (and it becomes difficult to see the benefits, because you forget that you're already doing it). Sometimes it's more of a way of thinking than a concrete way of doing.

基本上,任何,只要您的模型完全独立于GUI,就可以使用MVC的化身.

Essentially, any time your model is completely independent of your GUI, you are using some incarnation of MVC.

我注意到您也用观察者模式"标记了此标记,并简要提及了它.值得一提的是,至少在相对简单的应用程序中,为基于Swing的GUI显式实现观察者模式,只会增加一个冗余的抽象层,因为整个Swing API已经从根本上基于此设计模式了(这是整个过程)事件->侦听器子系统).

I noticed you also tagged this with "observer pattern" and mentioned it briefly. It's worth noting that many times, explicitly implementing an observer pattern for Swing-based GUIs, at least in relatively simple applications, adds nothing but a redundant abstraction layer, as the entire Swing API is already fundamentally based on this design pattern (it's the whole event -> listener subsystem).

我经常发现,使用周围的所谓观察者模式,如下所示的效果很好,其中控制器本质上只是事件侦听器的集合,例如:

Frequently I have found that something like the following works well, using the so-called observer pattern all around, where the controller was essentially just a collection of event listeners, like:

 public class Controller {
     Model m;
     public ActionListener getDeleteListener () {
         return new ActionListener() {
             @Override public void actionPerformed (ActionEvent e) {
                 m.deleteSomething();
             }
         };
     }
 }

然后GUI会执行以下操作:

And then the GUI does something like:

 public class GUI extends JFrame {
     JButton deleteButton; 
     public GUI (View v, Controller c) {
         deleteButton.addActionListener(c.getDeleteListener()); 
     }
 }

但是,有很多方法可以给猫咪蒙皮.在该示例中,即使您熟悉该概念,并且在适当的情况下,也可以采用快捷方式,只需让GUI构造函数注册用于修改模型的侦听器.然后,如上所述,控制器就变成了一个抽象概念.

But, there are many ways to skin that cat. In that example, even, once you are familiar with the concept, and if it is appropriate, you could take a shortcut and just have the GUI constructor register listeners that modify the model. Then, as mentioned above, the controller becomes just an abstract concept.

这篇关于MVC Java:控制器如何将侦听器设置为视图的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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