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?
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, etc. 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.
The MVC design pattern divides a software component to 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. Note that View / Controller is incorporated together, this is the usual usage of MVC design patterns, which provide components user interface (UI).
Examples of use of Button Detailed description
In order to better understand the relationship between MVC design patterns and Swing user interface components, let us analyze more inward. I will use the most common components button to explain. The behavior of a button's model should be completed by an interface buttonmodel. A button Model instance encapsulates the inside of its internal and defines the behavior of the button. All of it can be divided into four categories:
1. Query internal status 2, operate internal state 3, add and delete event listener 4, incident
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 instance
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, only the example can be limited to one user interface component.
Button class
The most obvious start is the code represents the code of the button component itself, 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; this.ButtonModel.AddActionListener (ButtonActionListener);
UpdateButton ();
Public void setui {ix (this.buttonui! = null) {this.buttonui.uninstallui (this);}
THIS.BUTTONUI = Buttonui;
IF (this.buttonui! = null) {this.buttonui.installui (this);}
UpdateButton ();
Public void updateButton () {invalidate ();
ButtonModel class
ButtonModel maintains three types of status information: whether it is pressed, whether "armed" is selected (SELECTED). They are all values of the Boolean type. A button is pressed (PRESSED) means that when the mouse is on the button, press the mouse but not release the mouse button, and the user drags the mouse to the outside of the button and does not change this state. A button is "armed" (armed) means that the button is pressed and the mouse is still on the button. Some buttons may also be selected (SELECTED), which takes the value of True or false by repeated click buttons.
The following code is a default implementation of state PRESSED. Status ARMED and the class of code and the class realized. The ButtonModel class should be inherited so that the default state definition can be overwritten to implement a personality button.
Private boolean boolpressed = false;
Public boolean ispressed () {return boolpressed;}
Public void setpressed (Boolean Boolpressed) {this.Boolpressed = Boolpressed; FirechangeEvent (New ChangEvent (Button));}
The View / Controller of the Buttonui class button is responsible for building a layer. By default it is just a rectangle with a background color, their subclasses have inherited them and covers the drawing method, so that the button can have many different performance, such as Motif, Windows 95, Java style, etc.
Public Void Update (Button Button, Graphics Graphics) {}
public void paint (Button button, Graphics graphics) {Dimension dimension = button.getSize (); Color color = button.getBackground (); graphics.setColor (color); graphics.fillRect (0, 0, dimension.width, dimension. HEIGHT);
The Buttonui class does not handle AWT events yourself, they use a custom event listener to translate the low AWT event into a senior Semantic event expected. Below is the code for installing / uninstalling the event listener.
Private static buttonuilistener buttonuilistener = null;
Public void installui (Button Button) {Buttonuilistener; Buttonuilistener; button.addchangelistener (Buttonuilistener);}
Public void Uninstallui (Button Button) {Buttonuilistener; Buttonuilistener; Buttonuilistener;
VIEW / Controller is actually some ways. They don't maintain any of your own status information. Therefore, many buttons can share a Buttonui instance. Buttonui is distinguished by adding a reference to a button in terms of parameter lists. Buttonuilistener class
The Buttonuilistener class can help the Button class to transform the mouse or keyboard input as a pair button model. This listener class implements: MouseListener, MouseMotionListener, ChangeListener interface, and processes events:
Public void mousedragged (mouseevent mouseevent) {Button Button = (Button) mouseevent.getsource (); ButtonModel ButtonModel = Button.getModel ();
IF (Button.getui (). Contains (Button, MouseEvent.getPoint ()) {ButtonModel.setarmed (TRUE);} else {buttonmodel.setarmed (false);}}}
public void mousePressed (MouseEvent mouseevent) {Button button = (Button) mouseevent.getSource (); ButtonModel buttonmodel = button.getModel (); buttonmodel.setPressed (true); buttonmodel.setArmed (true);}
public void mouseReleased (MouseEvent mouseevent) {Button button = (Button) mouseevent.getSource (); ButtonModel buttonmodel = button.getModel (); buttonmodel.setPressed (false); buttonmodel.setArmed (false);}
Public void statechanged (changing at changingeevent) [Button.getsource (); button.Repaint ();
to sum up
This example and the advantage of Swing user interface components are that you don't have to take time to figure out how the bottom is how to design it, it can be very convenient to use them. They all offer the default Model and View / Controller, then, when you do the components, you will find the power of the above ideas.