Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Creation and use of mouse event listeners

Shulou Source: shulou.com Published: 2022-06-01 05:49:21 10月04日 Update

Mouse operation is the most commonly used operation of the graphics operating system, users use mouse click, double click, right click, drag and other operations to interact with the software. Mouse event listener Mouse event listener is defined by MouseListener interface and MouseMotionListener interface, which define different mouse operation methods for mouse capture. MouseListener listener method description mouseClicked (MouseEvent e) handles mouse click event method

MouseEntered (MouseEvent e) execution method when the mouse enters the component area mouseExited (MouseEvent e) Mouse leaves the component area execution method mousePressed (MouseEvent e) execution method mouseRelease (MouseEvent e) when the mouse button is pressed mouseRelease (MouseEvent e) execution method when the mouse button is released

The method of MouseListener listener basically meets the needs of most programs.

The MouseMotionListener interface defines two handling methods for mouse movement and drag events. MouseMotionListener listener method description

MouseMoved (MouseEvent e) methods for handling mouse movement events mouseDragged (MouseEvent e) methods for handling mouse drag events the methods in the two mouse event listeners define formal parameters of MouseEvent type. The MouseEvent class is the mouse event class, which is the event object generated by the user action captured by the listener. The instance object contains a lot of parameter information when the mouse event occurs. For example, the coordinate position of the mouse, the mouse button and so on.

The common methods are: getButton () returns a mouse button with a changed state

GetClickCount () returns the number of mouse clicks associated with this event

GetLocationOnScreen () returns the absolute x _ focus y coordinates of the mouse relative to the screen

GetPoint () returns the x _ focus y coordinates of the event relative to the source component

TranslatePoint () demonstrates the function of the two interfaces by translating the event coordinates to the following code by adding the event coordinates to the specified xpeny offset. By reading the code, you will understand the role of their respective methods:

Import javax.swing.*

Import java.awt.event.*

Public class MyMouse extends JFrame {

Public JLabel jl = new JLabel ("no mouse operation")

Public MyMouse () {

SetBounds (100,100,350,80)

GetContentPane () .add (South, jl)

AddMouseListener (new MouseListener () {

Public void mouseClicked (MouseEvent arg0) {

Jl.setText ("Mouse clicked in the interface" + jl.getText () + arg0.getClickCount ()

+ "times")

}

Public void mouseEntered (MouseEvent arg0) {

Jl.setText ("Mouse entered the form interface")

}

Public void mouseExited (MouseEvent arg0) {

Jl.setText ("Mouse left the form interface")

}

Public void mousePressed (MouseEvent arg0) {

Jl.setText ("mouse pressed the key in the form interface" + arg0.getButton ())

}

Public void mouseReleased (MouseEvent arg0) {

Jl.setText ("mouse releases keys in the form interface" + arg0.getButton ())

}

});

SetDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)

}

Public static void main (String [] args) {

MyMouse test = new MyMouse ()

Test.setVisible (true)

}

}

The following code demonstrates the MouseMotionListener class, where the component can be dragged in the interface:

Import javax.swing.*

Import java.awt.FlowLayout

Import java.awt.event.*

Public class MyMouse extends JFrame {

Public JButton jb = new JButton ("mouse draggable button")

Public JTextField jt = new JTextField ()

Public MyMouse () {

Jb.setBounds (100,100,330175)

Jt.setColumns (20)

SetBounds (100,100,350280)

GetContentPane () .setLayout (new FlowLayout ()

GetContentPane () add (jb)

GetContentPane () add (jt)

AddMouseMotionListener (new MouseMotionListener () {

/ * *

* handle mouse drag events

* * /

Public void mouseDragged (MouseEvent arg0) {

MouseMoved (arg0)

Jb.setLocation (arg0.getPoint ())

}

/ * *

* handle mouse movement events

* * /

Public void mouseMoved (MouseEvent arg0) {

Jt.setText (arg0.getPoint () .toString ())

}

});

SetDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)

}

Public static void main (String [] args) {

MyMouse test = new MyMouse ()

Test.setVisible (true)

}

}

Tags: Mouse event method listener listener processing interface coordinates keys interface form component two code mobile location action region object commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology Redmi MariaDB Shulou Information