In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the addMouseListener () method in java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the addMouseListener () method in java".
The java addMouseListener () method uses the
The listener interface used to receive mouse events of interest (press, release, click, enter, or leave) on the component. (to track mouse movements and mouse drags, use MouseMotionListener. )
Use the component's addMouseListener method to register the listener object created from this class with the component. Mouse events are generated when the mouse is pressed, released, or clicked (pressed and released). Mouse events are also generated when the mouse cursor enters or leaves the component. When a mouse event occurs, the appropriate method in the listener object is called and the MouseEvent is passed to the method.
It contains five methods called when the public void mouseClicked (MouseEvent e) / / mouse button is clicked (pressed and released) on the component. Called when public void mouseEntered (MouseEvent e) / mouse enters over the component. Called when the public void mouseExited (MouseEvent e) / / mouse leaves the component. Called when the public void mousePressed (MouseEvent e) / / mouse button is pressed on the component. Called when the public void mouseReleased (MouseEvent e) / / mouse button is released on the component. Package mi;import java.awt.*;import javax.swing.*; public class mouseListener extends JFrame implements MouseListener {private JLabel j = new JLabel ("test text"); / / components used for testing private JTextArea jt = new JTextArea (); private JButton exit = new JButton ("exit"); public mouseListener () {j.addMouseListener (this); this.add (j, BorderLayout.NORTH); this.add (jt, BorderLayout.CENTER); this.add (exit, BorderLayout.SOUTH) This.setSize (500,400); this.setVisible (true); this.setLocationRelativeTo (null); exit.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {System.exit (0);}});} public static void main (String [] args) {new mouseListener () } @ Override public void mouseClicked (MouseEvent e) {/ / Action performed on mouse click jt.append ("Mouse clicked this text area\ n");} @ Override public void mouseEntered (MouseEvent e) {/ / Action performed when the mouse entered the component jt.append ("Mouse entered this text area\ n") } @ Override public void mouseExited (MouseEvent e) {/ / the action performed when the mouse leaves the component jt.append ("the mouse leaves this text area\ n");} @ Override public void mousePressed (MouseEvent e) {/ / the action performed when the mouse is pressed over the component jt.append ("you pressed the mouse in this text area\ n") } @ Override public void mouseReleased (MouseEvent e) {/ / the action performed on mouse release jt.append ("mouse button released\ n");}}
Alternatively, you can extend the abstract class MouseAdapter, where you only need to rewrite useful methods.
There are several other important ways
GetX (), getY (): get the coordinate position of the mouse
GetModifiers (): get the left or right mouse
GetClickCount (): gets the number of mouse clicks
SetCursor (): controls the shape of the mouse pointer
Java Mouse event Monitor MOUSEADAPTER
An action event occurs when the mouse button is pressed, released, or clicked (pressed and released)
Method summary
Called when the mouseClicked (MouseEvent e) mouse button is clicked (pressed and released) on the component.
Public class MouseAdapterTest extends Frame {public static void main (String [] args) {Frame f = new Frame (); f.setBounds (500,500,200,200); f.addMouseListener (new asd ()); f.setVisible (true);}} class asd extends MouseAdapter {@ Override public void mouseClicked (MouseEvent e) {System.out.println ("mouse click");}}
Thank you for your reading, the above is the content of "how to use the addMouseListener () method in java". After the study of this article, I believe you have a deeper understanding of how to use the addMouseListener () method in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.