The use of button listening and mouse listening in Java

Source: Internet
Author: User
Tags abstract

We are in the Java GUI Development, button and mouse monitoring is undoubtedly the use of the most important, and how to skillfully use these listeners, for future development is a great help, the following will be introduced separately.

How to implement a button listener:

Under Java.awt.event, there is a ActionListener class, which is a listener interface for receiving operational events that can be used to manipulate event events.

First we need to write a class to implement this interface, and then add a button response to our JButton object, and then add a listener to the button, the code is as follows:

   JButton bu=new JButton ("button");
     
   Bu.setactioncommand ("press");//Set Button response
     
   Bu.addactionlistener (listener);//Add button to monitor
     
import java.awt.event.ActionEvent;
     
Import Java.awt.event.ActionListener;
     
Publicclass Myactionlistener implements actionlistener{
     
   publicvoid actionperformed (ActionEvent e) {
     
      String Com=e.getactioncommand ()//Get button response
     
      if (com.equals ("press")) {
     
         System.out.println ("clicked on");
     
      }
     
   }
     
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Of course, the anonymous inner class can be used to add a listener to the button, here is no longer detailed, it should be noted that each button can be set by the Independent button response to achieve the function between the different buttons. Is it convenient?

Finished the button response, next to introduce the mouse response, we can implement a variety of functions on the form through the mouse, such as drawing board, Gobang, mouse games and so on, this is a very important GUI, this is a good way for the future development of a variety of applications have great help.

Don't say much nonsense, let's do it below:

First, there are two ways to implement mouse listening, one is to implement the Java.awt.MouseListener class, which is a listener interface for receiving "interested" mouse events (press, release, click, enter, or leave) on the component. Also to use the above ActionListener implementation method, specifically as follows:

       Import java.awt.event.MouseEvent;
     
Import Java.awt.event.MouseListener;
     
Publicclass Mymouselistener implements mouselistener{
     
   //mouse click event
     
   publicvoid mouseclicked (mouseevent e) {
     
      System.out.println ("mouseclicked");
     
   }
     
   Mouse Presses event
     
    publicvoid mousepressed (mouseevent e) {
     
       System.out.println ("mousepressed");
     
    }
     
    Mouse-Release Event
     
    publicvoid mousereleased (mouseevent e) {
     
       System.out.println ("mousereleased");
     
    }
     
    Mouse into Event
     
    publicvoid mouseentered (mouseevent e) {
     
       System.out.println ("mouseentered");
     
    }
     
    Mouse left Event
     
    publicvoid mouseexited (mouseevent e) {
     
       System.out.println ("mouseexited");
     
    }
     

Of course, this time we're going to add a listener to the form, so we should use the Addmouselistener () method for the form, but the method code is too verbose, and some of these methods aren't all we use every time, so here's another way is implemented by implementing the Mouseadapter class, an abstract class that can be inherited through other classes, and we can view his source code:

Public abstract class Mouseadapter implements MouseListener, Mousewheellistener, Mousemotionlistener

We found that this abstract class implements three interfaces, Mouselistener,mousewheellistener, and mousemotionlistener, so this abstract class implements all the methods in these three classes, and when used, We can just write the way we need to use, do we feel more flexible? Specific use, I will be in the production of simple drawing board mentioned, you may wish to try it.

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.