java practical tutorial - Components and event handling - MouseEvent event

Posted by miha on Sat, 25 Dec 2021 01:43:56 +0100

MouseEvent event

Mouse events can occur on any component, such as entering a component, exiting a component, clicking a mouse over a component, dragging a mouse, etc. all trigger mouse events, that is, the MouseEvent class automatically creates a mouse event
Event object. The method of registering the monitor for the event source is addMouseListener(MouseListener listener);

➊ use the mouseistener interface to handle mouse events

The MouseListener interface can handle mouse events triggered by the following five operations:
Press the mouse button on the event source.
Release the mouse button on the event source
Click the event source.
The mouse enters the event source.
The mouse exits the event source.

The MouseEvent class has the following important methods
getX() gets the x coordinate of the mouse pointer in the event source coordinate system.

getY() gets the y coordinate of the mouse pointer in the event source coordinate system.

● getModifiers() gets the left or right mouse button. The left and right mouse buttons use the constants BUTTONI MASK and button3 in the inputEvent class, respectively_ Mask
● getClickCount() gets the number of mouse clicks.
● getSource() gets the event source of the mouse event.
The MouseListener interface has the following methods:
● mousePressed(MouseEvent) is responsible for handling mouse events triggered by pressing the mouse button on the component. That is, when you press the mouse button at the event source, the monitor calls this method in the interface to handle the event.
● mouseReleased(MouseEvent) is responsible for handling mouse events triggered by releasing mouse buttons on components. That is, when you release the mouse button at the event source, the monitor calls this method in the press port to process the event.
● mouseEntered(MouseEvenr) is responsible for handling mouse events triggered by mouse entry components. That is, when the mouse pointer enters the component, the monitor calls this method in the interface to handle the event.
● mouseExited(MouseEvent) is responsible for handling mouse events triggered when the mouse leaves the component. That is, when the mouse pointer leaves the container, the monitor calls this method in the interface to handle the event.
● mouseClicked(MouseEvent) is responsible for handling mouse events triggered by clicking the mouse button on the component. That is, when you click the mouse button, the monitor calls this method in the interface I to process the event.

In the following example 10, monitor the mouse events on the button, text box and window respectively. When a mouse event occurs, obtain the coordinate value of the mouse pointer. Note that the upper left corner of the coordinate system of the event source is the origin.

public class Example9_10 {
	   public static void main(String args[]) {
	       WindowMouse win=new WindowMouse();
	       win.setTitle("Handle mouse events"); 
	       win.setBounds(10,10,560,380);
	   }
	}


import java.awt.*;
import javax.swing.*;
public class WindowMouse extends JFrame {
    /**
	 * 
	 */

	JButton button;
    JTextArea area;
    MousePolice police; 
    WindowMouse() {
       init();
       setVisible(true);
       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    }
    void init() {
       setLayout(new FlowLayout());
       area = new JTextArea(10,28);
       Font font = new Font("Song typeface",Font.PLAIN,22);
       area.setFont(font);
       police = new MousePolice();
       police.setView(this);//Pass the current window to the window of the police combination 
       button = new JButton("Button"); 
       button.addMouseListener(police);
       addMouseListener(police);
       add(button);
       add(new JScrollPane(area));
   }
}


import java.awt.event.*;
import javax.swing.*;
public class MousePolice implements MouseListener {
    WindowMouse view;
    public void setView(WindowMouse view) {
       this.view = view;
    }
    public void mousePressed(MouseEvent e) {
       if(e.getSource() == view.button&&e.getButton() == MouseEvent.BUTTON1) {
           view.area.append("Press the left mouse button on the button:\n");
           view.area.append(e.getX()+","+e.getY()+"\n");
       }
       else if(e.getSource() == view&&e.getButton() == MouseEvent.BUTTON1) {
           view.area.append("Press the left mouse button in the form:\n");
           view.area.append(e.getX()+","+e.getY()+"\n");
       }
    }
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e)  {
       if(e.getSource() instanceof JButton)
           view.area.append("\n Mouse entry button,position:"+e.getX()+","+e.getY()+"\n");
       if(e.getSource() instanceof JFrame)
           view.area.append("\n Mouse entry window,position:"+e.getX()+","+e.getY()+"\n"); 
    }
    public void mouseExited(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {
       if(e.getClickCount()>=2)
           view.area.setText("Mouse Combo\n");
   }
}

Topics: Java