Graphical description of Java Notes---event listener

Source: Internet
Author: User
Tags event listener

The event listener represents the interface responsible for handling events. Java provides a variety of event listener classes, but we'll talk about those event listener classes that are used more frequently. Each method of an event listener has a parameter as an object that is a subclass of the EventObject class. For example, a mouse event listener method will accept an instance of MouseEvent, where MouseEvent is EventObject derived.

Eventlistner interface

It is a labeled interface, and each listener interface must extend it. This class is defined in the Java.util package.

Event: When an event occurs, there is a corresponding processing scenario.
Event Source Listener Event handling scenario

The elements we added in the frame were clicked without any response, and we needed to add a listener.

Keyboard Event Listener

Import java.awt.Event. Keyadapter;import java.awt.Event. Keyevent;import javax.swing.jbutton;import javax.swing.jframe;import Javax.swing.jlabel;import javax.swing.JPanel; Import Javax.swing.JTextField; Public classDemo05 {Static intCount=0;  Public Static voidMain (string[] args) {JFrame frame=NewJFrame ("form"); JPanel Panel=NewJPanel (); JLabel namelable=NewJLabel ("User name"); JTextField namefiled=NewJTextField (Ten);        Panel.add (namelable);        Panel.add (namefiled);                Frame.add (panel); Namefiled.addkeylistener (NewKeyadapter () {@Override Public voidkeypressed (KeyEvent e) {System. out. println ("pressed characters:"+E.getkeychar ()); System. out. println ("gets the value corresponding to the key:"+E.getkeycode ());        }        }); Demo01.initjframe (Frame, -, -); }}

Graphical description of Java Notes---event listener

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.