使用Swing实现Java SE MVC [英] Java SE MVC implementation with Swing
问题描述
我使用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屋!