Java Swing learned some of the readers of the Swing relatively complex event-driven model is confusing, although the event-driven model in Java Swing was reflected entirely, but for a beginner in terms of software this sort of “naked “The event-driven model is very difficult to understand.

Microsoft Corporation. Net Framework and Java Swing for GUI programming is much simpler compared to the same event-driven model. Net framework to deal with a large number of packages,. Net commission called this package browser (Delegate) its code is as follows:

/ / When the button is clicked after btnSubmit asked to approach btnSubmit_Click
/ / EventHandler Kai in the middle to the commission’s role
/ / It is responsible for the distribution of the incident to a specific method to deal with
this.btnSubmit.Click + = new EventHandler (this.btnSubmit_Click);
/ / Event handling methods
/ / Object sender: event source, in this case the object btnSubmit
/ / EventArgs e: event handling parameters, save it to the programmers of the need to provide the necessary information
private void btnSubmit_Click (object sender, EventArgs e)
(
/ / This is a button to print statements
System.Diagnostics.Debug.WriteLine ( “This is button”);
)

In contrast, we take a look at the Java Swing event handling, and commissioning will be many complex: the code is as follows: (If you do not understand the Swing is event-driven, you can refer to my other article: Detailed examples of event-driven model ( Java chapter)):

/ / Listener for btnSubmit increase SelectHandler, when clicked after btnSubmit
/ / There is the actionPerformed listener to handle the Click event of the business
/ / Since the incident btnSubmit source and listener SelectHandler in two different categories of class
/ / In order to obtain SelectHandler pages categories, we need to form the object (this)
/ / Imported to the listener in
btnSubmit.addActionListener (new SelectHandler (this));
/ / Listener SelectHandler, it must be action to achieve the incident interface ActionListener
/ / In order to achieve the role of the distribution of the incident
class SelectHandler implements ActionListener (
Private CommonDialogDemo form = null;
/ / Will form the object through the constructor CommonDialogDemo incoming class SelectHandler
Public SelectHandler (CommonDialogDemo form) (
This.form = form;
)
/ / Event handler method, when clicked btnSubmit automatically print the following code
Publicvoid actionPerformed (ActionEvent e) (
System.out.println ( “This is button”);
)
)

Based on the above code, we can clearly see that the Java Swing than. Net of trouble more and more people can not put up with is that if there is more than one page button, we must prepare for each button a number of events detected listening categories, and these categories will be set within the general category. Modeling software to learn the readers may know, the internal model in the software engineering in the software is not recommended, therefore, the preparation of the code will increase significantly the design of redundancy and complexity, so we can consider themselves similar to the preparation of a . Net in the same event EventHandler to entrust the distribution of categories to deal with the incident.

Since we have no right to modify the Java compiler, so I will be here through the use of reflection technique, using an event-type commission to deal with all the click events, the code is as follows:

package cn.softworks.teachersearchsystem.support;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;

/ **
* Type is used to deal with all of the Swing button click events, and will be dealt with in accordance with the right to <br>
* To the user to deal with
*
* @ authorChen.yu
*
* /
publicclass EventHandlerimplements ActionListener (

/ / Components which form the object
Private Object form = null;

/ / By way of commission
Private String methodName = null;

/ **
* Constructor
*
* @ Paramform object components which form
* @ ParammethodName commissioned by the method of
* /
public EventHandler (Object form, String methodName) (
This.form = form;
This.methodName = methodName;
)

/ **
* Event handler methods to entrust
* /
Publicvoid actionPerformed (ActionEvent e) (

/ / Get the type of object form
Class formType = this.form.getClass ();

Try (
/ / Get the type of the specified method commissioned
Method method =
formType.getMethod (this.methodName, new Class [] (e.getClass ()});
/ / Call the specified method
method.invoke (this.form, new Object [] (e));

) Catch (Exception ex) (
Return;
)
)
)

Now we come to the preparation of a test procedure, the code is as follows:

btnSearch.addActionListener (new EventHandler (this, “btnSearch_Click”));

public void btnSearch_Click (ActionEvent e) (
System.out.println ( “This is btnSearch”);
)

Code from the above we can clearly see that the event handler and commissioned at the same event in the form,. Net to facilitate the handling of the Delegate reflection we realized.