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结构得以实现。
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结构得以实现。