Understanding JAVA event processing mechanism from scratch (3), from scratch java

Source: Internet
Author: User

Understanding JAVA event processing mechanism from scratch (3), from scratch java

We have written the example of teacher-student in two consecutive sections, and we will surely feel bored. In this example, we are playing for 100 times and still do not know how to write real code. From this section, we start to move closer to the real code.

The most understandable example of an event is a mouse event: We click the mouse to send instructions and execute code.

 

I. Mouse Click Event Processing Model Basic Edition

At this time, we must check the related types in JDK. Compared with the UML diagram in the previous section "Understanding JAVA event processing mechanism from scratch (2)", we soon found that MouseListener in JDK corresponds to HomeworkListener. In fact, we can also know through analysis, mouseListener inherits from EventListener. Now that the MouseListener interface is available, we must have an implementation class, which is assumed to be ConcreteMouseListener. You may wish to achieve this first:

Package com. zuikc. events;

Import java. awt. event. MouseEvent;
Import java. awt. event. MouseListener;

Public class ConcreteMouseListener implements MouseListener {

Public ConcreteMouseListener (){

}
@ Override
Public void mouseClicked (MouseEvent e ){
System. out. printf ("I Have Been clicked by {% s}, and MD itch is dead ~~ ", E. getSource (). toString ());
}

@ Override
Public void mousePressed (MouseEvent e ){
// TODO Auto-generated method stub
}

@ Override
Public void mouseReleased (MouseEvent e ){
// TODO Auto-generated method stub
}

@ Override
Public void mouseEntered (MouseEvent e ){
// TODO Auto-generated method stub
}

@ Override
Public void mouseExited (MouseEvent e ){
// TODO Auto-generated method stub
}

}

We add business code for the clicked event processor.

Event processor: the implementation method of the listener's specific implementation class is called the event processor.

What should we look at next? Of course, it's MouseEvent. MouseEvent, the classes in this JDK are relatively a little big, and there are a lot of constructor parameters, but it doesn't matter. Let's take a look at it. I'll first talk about how to use this class, that is, how to get new.

/*
* The new Component () {} Here is the event source obtained by event. getSource ().
*/
MouseEvent event = new MouseEvent (new Component () {}, 1, 1, 1, 2, 3, 4, false );

In actual and normal circumstances, there is no need to create a new MouseEvent. During JAVA runtime, the mouse clicks on the hardware will be captured, the instance object is generated for us at the underlying layer of the Virtual Machine (which will be analyzed below), but we are simulating it first at this moment, so it does not prevent us from making a new one randomly. Note that new is not a problem. The key to the problem must be the significance of its constructor parameters. The core key parameter is the first parameter, new Component (). What is this? This is the event source! Let's look back at where the student version of our instructors was produced:

Public void setHomework (String homework ){
System. out. printf ("% s assigned the job % s \ n", this. name, homework );
Homeworks. add (homework );
HomeworkEventObject event = new HomeworkEventObject (this );
/*
* In the observer mode, we call the observer yobservers of the Observable directly to notify the observer.
* Now we can only notify ourselves ~~
*/
For (HomeworkListener listener: homeworkListenerList ){
Listener. update (event, homework );
}

}

Is in the Teacher Business Code setHomework. So in the current example we want to write, where is the new MouseEvent? In the Business Code of the Button, the Button is similar to Teacher, but it is not exactly the same as Teacher. In Teacher, Teacher itself is the event source, therefore, this is passed into HomeworkEventObject as a parameter, and the Button cannot be passed into MouseEvent as a parameter. Because I do not want the Button to inherit from Component, we first create a temporary Component. OK. After analysis, our own Button code will probably come out, which is like this:

Package com. zuikc. events;

Import java. awt. AWTEvent;
Import java. awt. AWTEventMulticaster;
Import java. awt. Component;
Import java. awt. event. MouseEvent;
Import java. awt. event. MouseListener;
Import java. awt. peer. LightweightPeer;

Public class Button {
Private MouseListener mouseListener;
Public void addMouseListener (MouseListener l ){
MouseListener = l;
}
Public void doClick (){
/*
* The new Component () {} Here is the event source obtained by event. getSource ().
*/
MouseEvent event = new MouseEvent (new Component () {}, 1, 1, 1, 2, 3, 4, false );
// Event. getSource ();
This. mouseListener. mouseClicked (event );
}
}

Now, we can draw a clear class chart:

By the way, let's take a look at the Client code:

public static void main(String[] args) {
    ConcreteMouseListener listener = new ConcreteMouseListener();
    Button button = new Button();
    button.addMouseListener(listener);
    button.doClick();
}

Run it. You should get an output similar to this:

I was clicked by {com. zuikc. events. Button $1 [, 0, 0, 0x0, invalid]}, and MD was itchy ~~

 

2. What a normal form program looks like

Above, we try to look at the example of teachers and students to write the basic version of the mouse event, but what about the program? Let's write a normal program next. 99.9% is written as follows when a person writes a form program. I know someone else will scold you again. What, java, form programs? I learned JAVA from TMD for EE development and enterprise development. Now, let's talk about mutual harm first. We need to know that even NB, such as JAVA, is first originated from the form, and the JAVA form framework has been pushed down and rewritten more than once. Therefore, you understand the form events. The framework events in EE are almost the same as cutting cabbage.

Let's get down to the truth and read the code:

Package com. zuikc. events;

Import java. awt. event. MouseEvent;
Import java. awt. event. MouseListener;
Import javax. swing. JButton;
Import javax. swing. JFrame;

Public class Client {
Public static void main (String [] args ){
New DemoFrame ();
}
}

Class DemoFrame extends JFrame implements MouseListener {

Public DemoFrame (){
Super ("demo ");
This. setSize (500,400 );
This. setLocationRelativeTo (null );
This. getContentPane (). setLayout (null );
This. setVisible (true );

JButton button1 = new JButton ("OK ");
Button1.setBounds (8, 8, 80, 80 );
Button1.addMouseListener (this );
This. getContentPane (). add (button1 );
}

@ Override
Public void mouseClicked (MouseEvent e ){
System. out. printf ("I Have Been clicked by {% s}, and MD itch is dead ~~ ", E. getSource (). toString ());
}

@ Override
Public void mousePressed (MouseEvent e ){
}

@ Override
Public void mouseReleased (MouseEvent e ){
}

@ Override
Public void mouseEntered (MouseEvent e ){
}

@ Override
Public void mouseExited (MouseEvent e ){
}
}

What does this code mean? The simplest is to create a form, place a button on the form, and click it to execute a line of code. This simple file does not have many lines of code, but actually implements the functions implemented in a bunch of classes above. Let's analyze it and point out the listeners, event processors, events, and event sources.

Listener: DemoFrame is the listener, corresponding to ConcreteMouseListener;

Event processor: The MouseClicked method is the listener. This method is also available in ConcreteMouseListener;

Event: no more. What should I do?

Event Source: no more. What should I do?

Note that the form itself is a listener, so how to add a listener for the button in the above Code? Button1.addMouseListener (this); yes, it is to add itself.

Then, the event and event source are invisible. What should I do at this time? If we look at the output, the output of the above Code is:

I was {javax. swing. JButton [, 0.0, 80x80, invalid, alignmentX = 0.5, alignmentY =, border = javax. swing. plaf. borderUIResource $ CompoundBorderUIResource @ 7fda7dfe, flags = 296, maximumSize =, minimumSize =, preferredSize =, defaultIcon =, disabledIcon =, disabledSelectedIcon =, margin = javax. swing. plaf. insetsUIResource [top = 2, left = 14, bottom = 2, right = 14], paintBorder = true, paintFocus = true, pressedIcon =, roloverenabled = true, roloverivericon = , Roloverselectedicon =, selectedIcon =, text = OK, defaultCapable = true]} clicked, MD itch dead ~~

Similar to the output of the first part of the code above, it is also a variable generated during JButton business code running, but we do not know where it is generated or where it is generated. But it doesn't matter. Let's look at the debugging stack!

Step by step, we finally caught up here:

 

It can be seen that the MouseEvent is also new in the Business Code. You may want to, what about this important first parameter target? The target event source is also very important. The reason is very simple. We will continue to catch up with it, but it will not be expanded here. It will be new somewhere you want to see it.

Now let's make up the answer,

Event: A hardware mouse is captured during JAVA runtime to trigger the event processor. The MouseEvent generated inside the event processor is an event;

Event Source: The hardware mouse is captured during the JAVA runtime to trigger the event, and the event processor is called. The target generated inside the event processor is the event source;

 

Iii. Code of the first part of the normal Edition

What should the first part of the code look like?

When I put the first and second parts together for comparison, as long as I change the two parts, the code in the second part will be exactly the same as that in the second part,

1: Name ConcreteMouseListener DemoFrame;

2. Place the Button instance from the client to the ConcreteMouseListener;

OK, the event is so simple.

The first and second parts of the series are as follows:

1: Understand JAVA event processing mechanism from scratch (1)

2: Understand JAVA event processing mechanism from scratch (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.