Swing如何与MVC配合? [英] How does Swing fit in with MVC?

查看:54
本文介绍了Swing如何与MVC配合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Swing的MVC模式,网络上有很多示例.但是,似乎没有一种直接的做事方法.

There are many examples on the web when it comes to the MVC pattern with Swing. However, there doesn't seem to be a straightforward way of doing things.

以下是一些问题:

  1. View的概念可以直接与诸如JFrames的Swing组件相关联吗?
  2. 同样,Swing使用的现有模型类可以用作 the 模型吗?还是应该通过几个Swing模型类改编一个基本模型?
  3. 如果对2的回答是是",那么这些模型类应该集中在一个类中(属性/复合)还是分组在一个包中(每个单独的模型一个类)?
  4. 如果Model类保持分离,这是否意味着典型应用程序中可以同时运行多个MVC模式?
  5. 对于一个给定的模型,模型,视图和控制器之间的一对一对应关系是命令性的还是必须存在一个以上的控制器/视图?
  6. 最后,应如何构建应用程序以使其易于维护?
  1. Can the notion of View be directly associated with Swing components such as JFrames?
  2. Likewise, can the existing model classes that Swing uses be used as the Model or should one basic model be adapted through several Swing model classes?
  3. If the answer to 2 is yes, should these Model classes be centralized in one class (attribute/Composite) or grouped in a package (one class per separate Model)?
  4. If Model classes remain separate, does this mean that there can be several MVC patterns running at the same time in a typical application?
  5. Is a one-on-one correspondance between a model, a view and a controller an imperative or can more than one controller/view exist for one given model?
  6. Finally, how could an application be structured so it's easy to maintain?

某些链接:

  • Sun
  • Java2s
  • leepoint.net
  • Emilio Monti (decoupled Model and View)
  • Baptiste Witch (in french, uses Listener)
  • Site du zero (in french, uses Observer)

推荐答案

A JFrame,就像所有

A JFrame, like all top-level containers, makes an excellent view implementation; I typically use JPanel to organize the JComponent subclasses that constitute related view elements.

模型应不具有视图的 no 知识;通常会将状态更改通知观察者(听众),以响应其方法的调用.

The model should have no knowledge of the view(s); it notifies observers (listeners) of changes in state, typically in response to the invocation of its methods.

一个应用程序可能具有多个模型,并且每个模型可能具有多个视图.如可分离模型架构. > Swing体系结构概述 .

An application may have more than one model, and each model may have more than one view. Swing itself uses a related separable model architecture, as discussed in A Swing Architecture Overview.

这是一个非常简单的示例,您可能会从中受益.

Here's very simple example that you may find instructive.

这篇关于Swing如何与MVC配合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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