Java Event Processing Mechanism

Source: Internet
Author: User

Java Event Processing Mechanism

This article describes the Java event processing mechanism and focuses on some points of attention.

I. A preliminary introduction to the Java event processing mechanism (see the figure below)

According to the actual life, we can know that there can be multiple guardians, and the bad guys can operate on children by playing or loving.

Conclusion:

An event source does not mean that there is only one event listener. It can have multiple event listeners.

I know some basics. Some interfaces and classes are involved here. I will check them myself and will not repeat them here. At the bottom, there will be a code demonstration and a detailed explanation. If you are interested, you can use it to train your hands.

Ii. deep understanding of Java Event Processing Mechanism

1. Event programming steps:

① Event processing (event listener)

② Implement listening interfaces for event processing classes as needed

③ Rewrite (implement) the event processing function in the event processing class

④ In the event source class, specify the listener (listener) of the event, that is, register the listener.

2. code example (taking a simple Java GUI as an example)

Package com. bkjia;

// Function: Event Processing Mechanism demonstration 1.2
Import java. awt .*;
Import javax. swing .*;

Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. KeyEvent;
Import java. awt. event. KeyListener;
Import java. awt. event. MouseEvent;
Import java. awt. event. MouseListener;
Import java. awt. event. javaswevent;
Import java. awt. event. WindowListener; public class Demo1_3 extends JFrame {

Mypanl1_3 mp1 = null;

Public static void main (String [] args ){
// TODO Auto-generated method stub
New Demo1_3 ();
}

Public Demo1_3 (){

// I often forget this
Mp1 = new Mypanl1_3 ();

This. add (mp1 );

// Register the listener
This. addKeyListener (mp1 );
This. addMouseListener (mp1 );
This. addWindowListener (mp1 );
This. addMouseMotionListener (mp1 );
// This. addActionListener (mp1); // use this button, this. addActionListener (mp1 );

This. setSize (300,400 );
This. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
This. setVisible (true );

}
}

// This is often used. java. awt. event. MouseMotionListener move and drag the mouse
Class Mypanl1_3 extends JPanel implements java. awt. event. MouseMotionListener,
KeyListener, MouseListener, WindowListener {

Public void paint (Graphics g ){
Super. paint (g );
}

@ Override
Public void windowOpened (invalid wevent e ){
// TODO Auto-generated method stub
System. out. print ("opened window called" + e. getClass ());
}

@ Override
Public void windowClosing (WindowEvent e ){
// TODO Auto-generated method stub

}

@ Override
Public void windowClosed (WindowEvent e ){
// TODO Auto-generated method stub
System. out. println ("window closed ");

}

@ Override
Public void incluwiconified (incluwevent e ){
// TODO Auto-generated method stub

}

@ Override
Public void windowDeiconified (invalid wevent e ){
// TODO Auto-generated method stub

}

@ Override
// Window activation
Public void windowActivated (WindowEvent e ){
// TODO Auto-generated method stub
System. out. println ("window activation ");
}

@ Override
// The window is not activated.
Public void windowDeactivated (invalid wevent e ){
// TODO Auto-generated method stub
System. out. println ("Window not activated ");

}

@ Override
// 1. Click the mouse
Public void mouseClicked (MouseEvent e ){
// TODO Auto-generated method stub
System. out
. Println ("the abscissa of the current mouse click is" + e. getX () + "the ordinate of the current mouse click de is" + e. getY ());
}

@ Override
// 2. Press the mouse and the mouse is not released.
Public void mousePressed (MouseEvent e ){
// TODO Auto-generated method stub
System. out. println ("Mouse pressed, not released ");

}

@ Override
// 3. Release the mouse
Public void mouseReleased (MouseEvent e ){
// TODO Auto-generated method stub
System. out. println ("Release the mouse ");

}

@ Override
// 4. Move the mouse to MyPanel
Public void mouseEntered (MouseEvent e ){
// TODO Auto-generated method stub
System. out. println ("move the mouse to the panel ");

}

@ Override
// 5. Move the mouse away
Public void mouseExited (MouseEvent e ){
// TODO Auto-generated method stub
System. out. println ("move the mouse ");

}

@ Override
// 1. Key input (unlike keyPressed, keys in the peripheral circle do not respond)
Public void keyTyped (KeyEvent e ){
// TODO Auto-generated method stub

}

@ Override
// 2. Press the key (I tested it:
// Letters and a few keys did not respond, other keyboard outermost circle F1-F12, Delete console and so on have a reaction)
Public void keyPressed (KeyEvent e ){
// TODO Auto-generated method stub
// Note: switch to the American keyboard for demonstration. I demonstrated it in sogou input method, and the result is always incomplete.
System. out. println (e. getKeyChar () + "Press ");
}

@ Override
// Release the key
Public void keyReleased (KeyEvent e ){
// TODO Auto-generated method stub

}

@ Override
// Important: drag the mouse
Public void mouseDragged (MouseEvent e ){
// TODO Auto-generated method stub
System. out. println ("dragging the mouse ");

}

@ Override
// Important: move the mouse
Public void mouseMoved (MouseEvent e ){
// TODO Auto-generated method stub
// System. out. println ("move the mouse ");
System. out. println ("the current moving X coordinate is" + e. getX () + "the current moving y coordinate is" + e. getY ());
}
}

3. Notes:

① Java uses a delegation mechanism to process events

② Events in Java are classified (eg. mouse events, Form Events, keyboard events, etc)

③ If a class in Java wants to listen to an event, it must implement the corresponding event listening interface (does it come to the idea that Java is a "single inheritance multiple implementations" feature)

④ Rewrite the processing function in implementing the listener Interface Class (you should not be unfamiliar with the interface when it comes to this feature)

⑤ The event listening class must be registered in the event source. Otherwise, the event listening class cannot receive the event generated by the event source (just as if you want to enjoy protection, you need to grant the guardian the permissions of the Child guard)

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.