A good user interface (GUI) can usually find the corresponding performance in the real world. For example, if a simple button similar to the computer keyboard button is placed in front of your face, it is such a simple button, we can see the rules of a GUI design, which consists of two main parts. A portion allows it to have a button to have an action characteristic, for example, can be pressed. Another part is responsible for its performance, such as this button represents A or B. Seeing that these two points you discovered a very powerful design method, this method encouraged Reuse instead of redesigned Redesign. You find that the button has the same mechanism, you only need to create a "different" button on the top of the button, without re-designing a drawing for each button. This greatly reduces the time and difficulty of designing work. If you apply the above design ideas to the software development field, the achieved effect is not surprising. A very broad technology model in the software development area Model / View / Controller (MVC) is an implementation of this idea. This is of course very good, but maybe you start to doubt this and the user interface design section (SWING) in Java Foundation Class is related? Ok, let me tell you. Although the MVC design mode is usually used to design the entire user interface (GUI), JFC designers are originally used to design such design patterns to design a single component in Swing, such as form JTABLE, Tree JTree , Combined drop-down list box JCOMBOBOX, and more. These components have a Model, a view, a Controller, and these models, view, and controller can be changed independently, that is, when the components are being used. This feature enables the development of the toolbag for the GUI interface to be very flexible. Ok, come, let me tell you how it works. The MVC design mode is like I have just pointed out, MVC design mode divides a software component into three different parts, Model, View, Controller. Model is part of the component status and low-level behavior, which manages its own state and handles all the status of the status. Model does not know who uses your own view and controller, the system maintains the relationship between it and the relationship When MODEL has changed the system, it is also responsible for notifying the corresponding view. View represents a visual presentation of the data contained in Model. A Model can have more than one view, but there is little situation in swing. Controller manages the interaction between Model and users. It provides some way to process the situation when the Model changes. Use the example of the button on the keyboard to explain: Model is the entire mechanical device of the button, and View / Controller is the surface portion of the button. The following diagram explains how to divide a JFC development user interface into model, view, controller, note that View / Controller is incorporated together, this is the usual usage of MVC design mode, which provides components for user interface (UI) . Examples of using Button Detailed description In order to better understand the relationship between MVC design modes and Swing user interface components, let us analyze. I will use the most common components button to explain. We start from Model. The behavior that Model's Model should have by a button is completed by an interface buttonmodel. A button Model instance encapsulates the inside of its internal and defines the behavior of the button.
Its method can be divided into four categories: 1, query internal state 2, operating internal status 3, add and delete event listener 4, other user interface components have their respective user interfaces with components related model, but all Component Model provides these four types of methods. The view / controller of a button is done by an interface button in the picture above the view and Controller. If a class implements this interface, it will be responsible for creating a user interface to handle the user's operation. All of it can be divided into three major categories: 1, draw Paint2, return geometric type information 3, process other user interface components with their own components related to View / Controller, but they all provide the above three types of methods . Programmers will never be derived directly from Model and View / Controller, which is typically hidden in components inherited from java.awt.component, and these components are like a glue of the MVC three as one. It is also because of these inherited component objects, a programmer can make it easy to use Swing components and AWT components, then, we know that there are many Swing components to directly inherit the corresponding AWT components, which provides than AWT components. More convenient and easy to use, so usually, we don't have to use both. An example now we have understood the correspondence between Java classes and MVC, we can analyze problems more in place. Below we will tell a small example of using MVC mode development. Because JFC is very complicated, I can only limit my example in a user interface component (if you guess it is an example of a button, then you are right!) Let's take a look at all parts of this example. The BUTTON class is the most obvious place to represent the code of the button component, because this class is most of the programmer. As I mentioned earlier, button user interface component class is actually a binder between Model and View / Controller. Each button assembly is related to a Model and a Controller association, and Model defines the behavior of the button, and the View / Controller defines the performance of the button. The application can change these associations in any event. Let us see the code that implements this function. Public void setmodel (ButtonModel ButtonModel)
{
IF (this.buttonmodel! = null)
{
This.ButtonModel.RemoveChangelistener (ButtonChangelistener);
This.ButtonModel.RemoveActionListener (ButtonActionListener);
ButtonChangeListener = NULL;
ButtonactionListener = NULL;
}
THIS.BUTTONMODEL = ButtonModel;
IF (this.buttonmodel! = null)
{
ButtonChangelistener = new buttonchangelistener ();
ButtonactionListener = New ButtonActionListener ();
This.ButtonModel.addchangelistener (ButtonChangelistener);
This.ButtonModel.AddActionListener (ButtonActionListener);
}
UpdateButton ();
}
Public void setui (Buttonui Buttonui)
{
IF (this.buttonui! = null)
{
THIS.BUTTONUI.Uninstallui (this);
THIS.BUTTONUI = Buttonui;
IF (this.buttonui! = null)
{
This.Buttonui.installui (this);
}
UpdateButton ();
}
Public void updateButton ()
{
Invalidate ();
}
Before entering the next section, you should spend some time to read the source code of the Button class.