来讨论一下Swing中的MVC

Model/View/Controller Architecture 
 
There are a number of ways to approach using Swing to develop GUIs.  
As shown in the first part of this course, you can use most of the  
Swing widgets in the same way AWT widgets are used. If you take this  
approach, the transition from AWT programming is very easy.  
 
However, Swing gives the programmer a more powerful interface to the  
widgets. Employing a technology called the Model/View/Controller (MVC) 
architecture, the Swing team has given you the ability to control how  
widgets look, how they respond to input, and, for some more complex  
widgets, how data is represented.  
 
... 
 
Aside from a richer collection of widgets, the primary advantage of the  
Swing Component set over AWT is its use of MVC. MVC is a design pattern  
often used in building user interfaces (UI). In an MVC UI, there are  
three communicating objects, the model, view, and controller. The model  
is the underlying logical representation, the view is the visual  
representation, and the controller specifies how to handle user input.  
When a model changes, it notifies all views that depend on it. This  
separation of state and presentation allows for two very powerful features.  
 
1 You can base multiple views on the same model. For instance, you can  
  present a set of data in both table form and chart form. As you update  
  the data model, the model notifies both views and gives each an  
  opportunity to update itself.  
 
2 Because models specify nothing about presentation, you can modify or  
  create views without affecting the underlying model.  
 
A view uses a controller to specify its response mechanism. For instance,  
the controller determines what action to take when receiving keyboard input.  
 
Although the primary purpose of MVC is for building UIs, it can be used  
to establish an analogous notification protocol between non-visual objects.  
A model object can send change notifications to an arbitrary set of interested  
objects without knowing details about those objects. The Observer/Observable  
objects in java.util have served this need well, since Java 1.0.  
 
... 
 
我们常用的JTable JTree是非常典型的MVC构架。下图所示的内容是两个常用 
的MVC构架: 
 
       View              Controller               Model 
---------------------------------------------------------------------- 
     JTable              TableModelListener       DefaultTableModel 
     JTree               TreeModelListener        DefaultTreeModel 
 
从JDK1.1开始,事件接收和处理的方式发生了重大变化。JDK1.0采用的是层次 
事件模型:事件先发送到组件,然后沿容器层次向上传播,直到被处理为止。 
JDK1.1以后采用的是委托事件模型:事件是只向注册的Listener报告的对象, 
从没有注册的Listener的组件中发出的事件不会被广播。 
 
正是因为采用了委托事件模型,才使得MVC结构得以实现。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值