Javase Event Listener Interface

Source: Internet
Author: User

{Related Information}
In addition to providing methods to implement listener interfaces for handling events, Java provides another simple means of implementing listeners-event adapters (Eventadapter). The programmer can inherit the adapter class corresponding to the event,

Rewrite the method of interest. Program code can be shortened through the event adaptation class, but Java can only implement a single inheritance, and the event adapter method cannot be used when the program needs to capture multiple events.

The event adapter classes defined in the Java.awt.event package include the following:

Componentadapter (Component adapter)
Containeradapter (Container adapter)
Focusadapter (Focus adapter)
Keyadapter (keyboard adapter)
Mouseadapter (Mouse adapter)
Mousemotionadapter (Mouse motion adapter)
Windowadapter (Window adapter)

{Sample sample Diagram}
Function Description: The panel adds listener Mouseadapter and overloads the Mousepressed and Mousereleased methods. In the Mousepressed method,

Gets the coordinates of the mouse and saves it. In mousereleased, get the coordinates of the mouse again, and then draw a line based on the coordinates you get before and after.

{Related Code}

 PackageSup.orange.learn;Importjava.awt.*;ImportJava.awt.event.MouseAdapter;Importjava.awt.event.MouseEvent;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;ImportJava.awt.image.ImageObserver;ImportJava.text.AttributedCharacterIterator;/*** Created by Re-x on 11/2/14.*/ Public classAdapterdemo {PrivateFrame F;    Point start, end;    Panel panel;  PublicAdapterdemo () {f=NewFrame ("Click or drag Mouse"); Panel=NewPanel (); F.add ("Center", panel); Panel.addmouselistener (NewMouseadapter () { Public voidmousepressed (MouseEvent e) {start=E.getpoint (); System.out.println ("Start" +start); }             Public voidmousereleased (MouseEvent e) {End=E.getpoint (); System.out.println ("End" +end); Graphics g=Panel.getgraphics ();                Panel.paint (g);            G.drawline (Start.x, Start.y, End.X, END.Y);        }        }); F.addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {Super. windowclosing (e); System.exit (1);        }        }); F.setsize (300, 400); F.setvisible (true); }     Public Static voidMain (string[] args) {NewAdapterdemo (); }}

Javase Event Listener Interface

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.