Java event listening mechanism and Java event listening Mechanism

Source: Internet
Author: User

Java event listening mechanism and Java event listening Mechanism

Three aspects of the mouse event listening mechanism:

1. Event source object:

The event source object is the object that can generate actions. All container components and element components in Java are event source objects in event listening. Java identifies different event source Objects Based on the action of the event. The component where the action occurs is the event source object.

2. event listening method:

AddMouseListener (MouseListener ml, it is handled by the event processing class (implementing the MouseListener interface.

AddActionListener (ActionListener al); capture the mouse clicking action on a button-like component or press ENTER action on a keyboard similar to the input box component, and then submit the action and related information to the event processing class (implementing the ActionListener Interface).

AddMouseMotionListener (MouseMotionListener mml); captures the movement and dragging of the mouse; then submits the action and related information to the corresponding method of the event processing class (implementing the MouseMotionListener Interface) for processing.

AddKeyListener (KeyListener kl); captures the pressing, releasing, and hitting actions of the keyboard buttons on the event source object, and submits the actions and related information to the event processing class (implementing the KeyListener interface).

3. Event interface (event processing class, that is, the class that implements the event Interface ):

MouseListener interface for mouse events, including pressing, releasing, clicking, and handling of incoming and outgoing events

ActionListener Action event interface, which has the corresponding action event processing method

MouseMotionListener: an interface for moving and dragging events

KeyLisetener keyboard event interface, which can be used to handle release, press, and strike events

After learning about the three aspects of event listening, it is easy to add event listening to components. Below is a small example:

Objective: to create a simple drawing board, you can press the corresponding function to achieve different painting purposes.

Analysis: 1. A simple interface of the drawing board is preferred, and a Draw class is defined to initialize the interface using the Swing component of Java.

2. Define the DrawListener class of the event interface to implement the above interface and override the methods in the interface to achieve its own purpose.

3. instantiate the DrawListener Class Object in Draw.

4. Add an event listening method for components in the Draw class, and specify the event processing class as DrawListener.

Code implementation:

Draw class

Package com. cbs; import java. awt. color; import java. awt. dimension; import java. awt. flowLayout; import java. awt. graphics; import java. awt. event. actionEvent; import java. awt. event. actionListener; import javax. swing. JButton; import javax. swing. JFrame;/*** Draw class for interface initialization ** @ author CBS **/public class Draw {public static void main (String [] args) {Draw t = new Draw (); t. showUI () ;}// interface initialization method public void showUI () {JFrame jf = new JFrame (); jf. setTitle ("Drawing"); jf. setSize (700,700); jf. setdefaclocloseoperation (3); jf. setLocationRelativeTo (null); FlowLayout layout = new FlowLayout (FlowLayout. LEFT); jf. setLayout (layout); JButton drawLine = new JButton ("draw a straight line"); jf. add (drawLine); JButton drawOval = new JButton ("draw an ellipse"); jf. add (drawOval); JButton drawArc = new JButton ("Draw curve"); jf. add (drawArc); JButton drawPolygon = new JButton ("Triangle"); jf. add (drawPolygon); JButton jb1 = new JButton (); jb1.setBackground (Color. RED); jf. add (jb1); jb1.setPreferredSize (new Dimension (30, 30); JButton jb2 = new JButton (); jb2.setBackground (Color. GREEN); jf. add (jb2); jb2.setPreferredSize (new Dimension (30, 30); jf. setVisible (true); Graphics g = jf. getGraphics (); // obtain the current paint brush DrawListener dl = new DrawListener (g); // instantiate the DrawListener Class Object jf. addMouseListener (dl); // Add the mouse event listening method jf for the form. addMouseMotionListener (dl); // Add the mouse movement event listening method for the form // Add the action listening drawLine for the button. addActionListener (dl); drawOval. addActionListener (dl); jb1.addActionListener (dl); jb2.addActionListener (dl); drawArc. addActionListener (dl); drawPolygon. addActionListener (dl );}}

DrawListener class

Package com. cbs; import java. awt. color; import java. awt. graphics; import java. awt. image; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. mouseEvent; import java. awt. event. mouseListener; import java. awt. event. mouseMotionListener; import javax. swing. imageIcon; import javax. swing. JButton;/*** event processing class ** @ author CBS **/public class DrawListener implements MouseListener, MouseMotionListener, ActionListener {private int x1, y1, x2, y2; // record the two mouse clicks on the coordinate private Graphics g; // obtain the canvas object private String str from the interface; // record the information of the current button to distinguish the private Color color of Different buttons; // record the paint brush color information private int f = 1; // control variable, used to update the coordinate public DrawListener (Graphics g) {this. g = g ;}// processing method when the mouse is pressed public void mousePressed (MouseEvent e) {// record the position of the first click; get if (f = 1) From Object e) {x1 = e. getX (); y1 = e. getY () ;}// Processing Method for Mouse clicking public void mouseClicked (MouseEvent e) {if ("Triangle ". equals (str) {System. out. println ("sanjaioxing"); int x, y; x = e. getX (); y = e. getY (); g. setColor (color); g. drawLine (x, y, x1, y1); g. drawLine (x2, y2, x, y); f = 1 ;}/// Processing Method for mouse release public void mouseReleased (MouseEvent e) {// record the coordinates when the mouse is released if (f = 1) {x2 = e. getX (); y2 = e. getY () ;}// two coordinates are obtained. It can be used to draw a straight line, call the g method of the canvas object, and draw a straight line on the interface if ("draw a straight line ". equals (str) {g. setColor (color); g. drawLine (x1, y1, x2, y2);} if ("draw an ellipse ". equals (str) {g. setColor (color); g. drawOval (x1, y1, x2, y2);} if ("Triangle ". equals (str) & f = 1) {g. setColor (color); g. drawLine (x1, y1, x2, y2); f ++ }}// Processing Method for mouse Entry public void mouseEntered (MouseEvent e) {} // Processing Method for mouse exit public void mouseExited (MouseEvent e) {} public void actionreceivmed (ActionEvent e) {if ("". equals (e. getActionCommand () {JButton jb = (JButton) e. getSource (); color = jb. getBackground ();} else {str = e. getActionCommand () ;}// processing method when dragging the mouse public void mouseDragged (MouseEvent e) {if ("Draw curve ". equals (str) {int x, y; x = e. getX (); y = e. getY (); g. setColor (color); g. drawLine (x1, y1, x, y); x1 = x; y1 = y ;}/// the move method when the mouse is released. public void mouseMoved (MouseEvent e ){}}

Ps: It's just a bit of an eye. please correct me.

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.