Through Java Swing to see the MVC design model (2)

xiaoxiao2021-03-06  99

The 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;

Firechangeeevent (New ChangEvent (Button);

}

The model of the button is also responsible for notifying the other objects (event listeners) they are interested in events. 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.AddeElement (ChangeListener);

}

Public void RemoveChangelistener (Changelistener ChangeListener)

{

VectorChangelisteners.RemoveElement (ChangeListener);

}

Protected void FirechangeEvent (ChangeEvent Changevent)

{

ENUMERATION Enumeration = VectorChangelisteners.efficient ();

While (Enumeration.haASMoreElements ())

{

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

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

Button.RemoveChangelistener (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. The Buttonuilistener class 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 mousedraged (MouseEvent MouseEvent)

{

Button button = (button) mouseevent.getsource ();

ButtonModel Buttonmodel = Button.getModel ();

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 mouseeleased (MouseEvent MouseEvent)

{

Button button = (button) mouseevent.getsource ();

ButtonModel ButtonModel = Button.getModel (); ButtonModel.SetPressed (false);

ButtonModel.setarmed (false);

}

Public void statechanged (changingeevent changingevent)

{

Button button = (button) Changeevent.getsource ();

Button.repaint ();

}

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

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

New Post(0)