Through Java Swing to see the MVC design mode

zhaozj2021-02-16  49

Through Java Swing to see the MVC design mode

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, 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.

Ok, come, let me tell you how it works.

MVC design mode

As I did, MVC design mode divided a software component into three different parts, Model, View, Controller.

Picture 1. MVC design mode

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) .

Figure 2: JFC user interface components

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.

We start from Model.

MODEL

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:

l Query internal status l Operation internal status

l Add and delete event listeners

l occurrence event

Other user interface components have their respective MODELs associated with components, but all component models provide these four types of methods.

View & Controller

The View / Controller of a button is done by an interface button. If a class implements this interface, it will be responsible for creating a user interface to handle the user's operation. Its method can be divided into three categories:

l Draw Paint

l Back to geometric type information

l Treatment AWT event

Other user interface components have their own components related 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 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, 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.

Button class

The most obvious beginning is the code represents the code of the button component, as 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 (button.buttonui! = null) {this.buttonui.uninstallui (this);} this.buttonui = buttonui; if (this.Buttonui! = null) {this.buttonui.installui (this.) UpdateButton (); public void updateButton () {invalidate ();} Before you enter the next section, you should spend some time to read the source code of the Button class.

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 ChangeEvent (button));} button button model model also responsible for notifying other objects (Event Listener) The events they are interested in. From the following bought, we can see a ChangeEvent when the button turns changes. Below is the code:

private Vector vectorChangeListeners = new Vector (); public void addChangeListener (ChangeListener changelistener) {vectorChangeListeners.addElement (changelistener);} public void removeChangeListener (ChangeListener changelistener) {vectorChangeListeners.removeElement (changelistener);} protected void fireChangeEvent (ChangeEvent changeevent) {Enumeration enumeration = vectorChangeListeners.elements (); while (enumeration.hasMoreElements ()) {ChangeListener changelistener = (ChangeListener) enumeration.nextElement (); changelistener.stateChanged (changeevent);}}

Before entering the next section, you should spend some time to read the source code of the ButtonModel class.

Buttonui class

The View / Controller of the button is responsible for building a delay. 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) {button.addMouseListener (buttonuilistener); button.addMouseMotionListener (buttonuilistener); button.addChangeListener (buttonuilistener);} public void uninstallUI (Button button) {button.removeMouseListener (buttonuilistener Button.RemoveMouseMotionListener (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. Similarly, I hope you can spend more time to see the Buttonui class, then let us enter the next section.

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 (buttonmodel.isPressed ()) {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); false;} public void statechanged (callton button = (button) Changeevent.getsource (); button.repai NT ();} I hope you can read the source code of Buttonuilistener before entering the next section. to sum up

I hope that you can do it in accordance with the methods described above. If you can't, then all efforts will be in a white fee. 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.

Attachment: Button.java Buttonui.java ButtonModel.java Buttonuilistener.java

转载请注明原文地址:https://www.9cbs.com/read-23214.html

New Post(0)