使用Swing实现Java SE MVC [英] Java SE MVC implementation with Swing

查看:208
本文介绍了使用Swing实现Java SE MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Swing使用 PropertyChageSupport PropertyChageListener 为Java SE实现了MVC模式。实现MVC的图表如下。

I've implemented MVC pattern for Java SE with Swing using PropertyChageSupport and PropertyChageListener. The diagram for implemented MVC is as follows.

View 的实现中,我在模型中进行属性更改控制器的帮助下。

In the implementation of View, I do property change in Model with the help of Controller.

查看包含类似以下代码的Ok按钮。

View contains code like following for Ok button.

btnOk.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        modelController.setNumber(Integer.parseInt(numberField
            .getText()));
        modelController.setName(nameField.getText());
    }
});

完整代码可在 SwingMVC

现在,我的问题是,我是否为 btnOk in 查看或者我应该在 Controller 所以在查看中,我将会这样做

Now, My question is, Do I write above code for btnOk in View or Should I write it in a method in Controller so that in View, I'll be doing

btnOk.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        modelController.btnOkActionPerformed();
    }
});

在以上两种实现中,哪种是实现MVC的首选方式?

推荐答案

首先要注意的是:我不是专业人士或学生,而是一个爱好者,但话虽如此,我自己的偏好是第二个例子,

First a caveat: I'm not a professional or student but a hobbiest, but having said that, my own preference is with your second example,

btnOk.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        modelController.btnOkActionPerformed();
    }
});

控件必须调用视图上的方法来提取信息,并且它将调用的任何方法都会成为视图实现的接口的一部分。我的目标是尽可能保持视野愚蠢,并做几乎任何事情以尽可能地松开耦合。

The control would have to call methods on the view to extract information, and any methods it would call would be part of an interface that the view implements. My goal in this is to keep the view as dumb as possible and do almost anything to loosen coupling as much as possible.

这篇关于使用Swing实现Java SE MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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