Java event processing mechanism (in-depth understanding), java in-depth understanding
This article describes the Java event processing mechanism and focuses on some points of attention.
I. Preliminary introduction to Java Event Processing Mechanism(View the image)
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. fanghua; // function: demonstrate the event processing mechanism 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. using wevent; import java. awt. event. windowListener; public class Demo1_3 extends JFrame {Mypanl1_3 m P1 = null; public static void main (String [] args) {// TODO Auto-generated method stub new Demo1_3 ();} public Demo1_3 () {// here we often forget 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. setDe FaultCloseOperation (JFrame. EXIT_ON_CLOSE); this. setVisible (true) ;}/// this is often used, java. awt. event. mouseMotionListener move the mouse and drag the 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 ("enable window Port called "+ e. getClass () ;}@ Override public void windowClosing (expose wevent e) {// TODO Auto-generated method stub} @ Override public void windowClosed (expose wevent e) {// TODO Auto-generated method stub System. out. println ("window closed");} @ Override public void protected wiconified (protected wevent e) {// TODO Auto-generated method stub} @ Override public void windowDeiconified (protected wevent e) {// TODO Auto-gener Ated method stub} @ Override // window activates public void windowActivated (invalid wevent e) {// TODO Auto-generated method stub System. out. println ("window activation");} @ Override // The Window does not activate public void windowDeactivated (invalid wevent e) {// TODO Auto-generated method stub System. out. println ("Window not activated");} @ Override // 1. click public void mouseClicked (MouseEvent e) {// TODO Auto-generated method stub System. out. println ("current mouse The abscissa of the click is "+ e. getX () + "The Y coordinate of the current mouse clicking de is" + e. getY () ;}@ Override // 2. press the mouse and do not release public void mousePressed (MouseEvent e) {// TODO Auto-generated method stub System. out. println ("Mouse pressed, not released");} @ Override // 3. release public void mouseReleased (MouseEvent e) {// TODO Auto-generated method stub System. out. println ("Mouse release");} @ Override // 4. move the mouse to MyPanel public void mouseEntered (MouseEvent e) {// TODO Auto-gener Ated method stub System. out. println ("move the mouse to the panel");} @ Override // 5. move the mouse away from 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: // letters and a few keys did not respond, other keyboard outermost F1-F12, Delete and other console have a reaction) public void keyPressed (KeyEven T 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 System. out. println (e. getKeyChar () + "Key press") ;}@ Override // key to release public void keyReleased (KeyEvent e) {// TODO Auto-generated method stub} @ Override // important: drag public void mouseDragged (MouseEvent e) {// TODO Auto-generated method stub System. out. println ("dragging the mouse");} @ Override // important: move the mouse over public void mouseMoved (MouseEvent e) {// TODO Auto-generated method stub // System. out. println ("move the mouse"); System. out. println ("the current moving X-axis is" + e. getX () + "the current moving ordinate 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)