Detailed explanation of mouse event design in java graphical programming
The event source of mouse events is often related to the container. Mouse events will occur when the mouse enters the container, leaves the container, or clicks or drags the mouse in the container. The Java language provides two interfaces for handling mouse events: mouselistener and mousemotionlistener. Mouselistener interface
Mouselistener interface can handle five kinds of mouse events: press the mouse, release the mouse, click the mouse, enter the mouse and exit the mouse. The corresponding methods are: (1) geTx (): the X coordinate of the mouse (2) gety (): the Y coordinate of the mouse (3) getmodifiers (): get the left or right button of the mouse. (4) Getclickcount(): the number of mouse clicks. (5) Getsource(): get the source of the mouse event. (6) AddMouseListener: Add / drop monitor. (7) Removemouselistener: remove the monitor.
The methods of mouselistener interface to be implemented are: (1) mousePressed (mouseevent E); (2) mouseReleased(MouseEvent e); (3) mouseEntered(MouseEvent e); (4) mouseExited(MouseEvent e); (5) mouseClicked(MouseEvent e);
[example] the applet has a text area for recording a series of mouse events. When the mouse enters the applet window, the text area displays "mouse in"; when the mouse leaves the window, the text area displays "mouse away"; when the mouse is pressed, the text area displays "mouse down"; when the mouse is double clicked, the text area displays "mouse double click" ; And display the coordinates of the mouse. The program also displays a red circle. When you click the mouse, the radius of the circle will continue to increase.
Mouse events can occur on any component: mouse entry, mouse exit, mouse press, etc. For example, add a button in the above program, add a mouse monitor to the button object, and modify the init () method in the above program to the following form, that is, all mouse events on the button can be indicated.
If the program wants to know further that the left or right mouse button is pressed or clicked, the left or right mouse button can be used with the constant button1 in the inputevent class_ Mask and button3_ Mask to determine. For example, the following expression determines whether the right mouse button is pressed or clicked:
Mousemotionlistener interface
The mousemotionlistener interface handles two events: dragging the mouse and moving the mouse.
The method of registering the monitor is: addMouseMotionListener (monitor). There are two interface methods to be implemented: (1) mousedragged (mouseevent E) (2) mousemoved (mouseevent E)
[example] an application in which the scroll bar changes synchronously with the display window. The window has a box. Drag the box with the mouse, or click the window with the mouse, and the box changes the display position. The sliders of the corresponding horizontal and vertical scroll bars will also change their position in the scroll bar. On the contrary, moving the slider of the scroll bar will also change the display position of the box in the window.
In the above example, if you only need to change the display position of the content by sliding the slider, you can simply use the scroll panel JScrollPane. If so, the creation and control of scroll bars can be eliminated and implemented directly inside JScrollPane. See the following modified definition of mywindow:
The mouse pointer shape can also be controlled by the program. The SETCURSOR () method can set the mouse pointer shape. For example, the code SETCURSOR (cursor. Getpredefinedcursor (cursor. Wait_cursor)).
