Maybe every software practitioner has a transformation process from learning console applications to Learning Visual Programming. The advantage of console applications is that they can easily practice the syntax and Development habits of a language (such. net and Java), and Visual Programming learning can be very convenient to develop a variety of man-machine conversation interface (HMI ). Visual Programming may be the beginning of a beginner's interest in software, or a milestone in software learning, because we can use various integrated software development environments (IDE) you can easily drag and drop components in ready-made interface windows, including common buttons, single-choice buttons, and check boxes ). This drag-and-drop development method is not only convenient, but also displays the window in front of us immediately. This may be a great sense of accomplishment for a software beginner.
However, many software learners only have a superficial understanding of Visual Programming in the process of learning visual development. They may think that it is very commendable to be able to use the drag-and-drop method to complete an interface, however, few seriously understand the support and integration of visual programming components by programming languages, therefore, the following describes the most important "event-driven model" in the Visual Programming process based on my teaching experience ".
1.What is an event-driven model?
Before explaining the event-driven model, let's look at the three main elements of the event-driven model:
Event Source: the source body that can receive external events.
Listener: an object that can receive event source notifications.
Event Handler: the object used to process events.
Students should understand that any event-driven model-based development technology includes the above three elements, no matter. net or Java technology, and even the previously used Visual Basic and Delphi language both have an event-driven model development process based on the above three elements.
Now let's look at an example in our life. If one day you walk on the road, you will be accidentally knocked down by the vase from the sky, and you will be dizzy. The entire process is actually an event processing process, and we can easily analyze the three main elements of the event-driven model just mentioned.
1. The stunned person is actually the event source, because he is the source body that can receive external events.
2. The Listener is the brain nerve of this person, because it will perceive the pain.
3. The incident is handled by this person dizzy.
Because event-driven models are everywhere in our daily lives, Java and other programming languages have applied this process to visual programming.
2.Event-driven model in Java programming language
Java swing is the most common Visual Programming Technology in Java programming. Java swing provides developers with many ready-made components, such as buttons (jbuttons) and single-choice buttons (jradiobutton). To manage the interaction between users and components that make up the graphic user interface of the program, you must understand how to handle events in Java.
Assume that you have clicked a button in the graphic user interface of the program. In fact, this button is the source of the event (the object that can trigger the event ). All Java swing objects have the ability to perceive their own operations, so jbutton also has the ability. An event usually requires a source object, which is a jbutton object. When you click a button, the jbutton component class generates an actionevent object for storing the event parameters. This object contains the event and event source information. Figure 1-1 shows this mechanism.
Figure 1-1
When the jbutton detects that it is clicked, it will pass this feeling to a listener object, which has been notified of interest in such events, A listener object is only an object that listens for a specific event. Here, "passing events to listeners" only means that the event source calls a specific method in the listener object and uses the event object as the real parameter. A listener object can listen on events of a specific object (such as a button ).
In fact, any class object can be used as a listener object, as long as the class implements the listener interface. You will find a variety of listener interfaces to meet the needs of different types of events. In this example, You need to implement the actionlistener interface to receive button events. In the method declared by the listener interface, the code that accepts the event object and responds to the event is implemented. In this example, when an event occurs, the actionreceivmed () method in the actionlistener interface is called. Each listener interface defines a specific method to receive the events to be processed by the listener.
Only implementing the listener interface is not enough to connect the listener object to the event source. You still need to connect the listener to a single source or multiple sources of the event you want to process. You can register a listener object with an event source by calling a specific method of the event source object. For example, to register a listener that listens for button events, you need to call the addactionlistener () method of the jbutton object. This operation can bind the listener object to the event source.
Each event response only involves listeners interested in the event. Because the listener only requires the implementation of a suitable interface, in fact, you can receive and process events wherever you want. In Java, the method of using a listener object to process events is called the delegated event model. This is because the Event Response Caused by a component such as a button is not handled by the object that causes the event, instead, it delegates an independent listener object for processing. The actionreceivmed () is actually a delegate processing method. Now let's take a look at how jbutton converts users' clicks into Method Processing (1-2 ).
Figure 1-2
Jbutton component initialization code snippet:
Private voidInitialize (){
Frame =NewJframe ();
Frame. getcontentpane (). setlayout (Null);
Frame. setbounds (100,100,247,165 );
Frame. setdefaclocloseoperation (jframe.Exit_on_close);
Frame. settitle ("event driver ");
// Btnpress is the event source in this click operation
Btnpress =NewJbutton ();
Btnpress. settext ("Press ");
Btnpress. setname ("Press ");
Btnpress. setbounds (63, 98, 99, 23 );
// Implant the listener object buttoneventhandler into the event source btnpress
Btnpress. addactionlistener (NewButtoneventhandler (This));
Frame. getcontentpane (). Add (btnpress );
Frame. getcontentpane (). Add (txtmessage );
}
Code snippet created by the listener:
// Listener object buttoneventhandler (used to listen for Button clicking operations)
ClassButtoneventhandlerImplementsActionlistener {
// Form object
PrivateEventdemo form =Null;
// Input a form object through the constructor,
// The function is to make the listener object understand that the event source is in
// In which form container
PublicButtoneventhandler (eventdemo form ){
This. Form = form;
}
// Delegate Method
Public voidActionreceivmed (actionevent e ){
// This method will give the event processing permission to the form container class
// Btnpress_click method.
This. Form. btnpress_click (E );
}
}
Real event processing code snippets:
/**
* The event handling method of the button btnpress.
*
*@ ParamE event parameters
*/
Private voidBtnpress_click (actionevent e ){
String message = "the name of the button you clicked is :"
+ (Jbutton) E. getsource (). getname ();
This. Txtmessage. settext (Message );
}
The event handler code snippet of the button:
Code Working principle:
The initialization code snippet of the jbutton component clearly states that the button is placed on the form after it is created. The key lies in the following statement of the code snippet:
Btnpress. addactionlistener (NewButtoneventhandler (This));
This statement embeds the listener object buttoneventhandler into the event source, which implements the actionlistener structure. Therefore, the jbutton Class Object btnpress has the ability to process the buttons clicked by users.
When you click the btnpress button, the button object will directly pass the click feeling to the actionreceivmed method of buttoneventhandler for processing, in order to develop better programming habits, our center does not recommend that students write code directly in the delegate method. Instead, they need to process the event and forward it to a method in the form for processing. The naming of this method must also be rule-based, it is the event source name + underline + event name (btnpress_click), and this method must have the event parameter actionevent, because it is clearly indicated in this object, which button is clicked. E. the getsource () method returns the object of the clicked button. Because this time a button is clicked, we need to use jbutton. the return value of getsource () is strongly converted, and then the button name is obtained through the getname () method. Now the entire click event has been processed.
This article mainly aims to explain the Java event processing mechanism. As for the internal working principles of the listener and event source, I will explain in detail in my next technical article "Observer mode of Java", To be continued ......