"Fundamentals of Java language Programming"--event-driven programming--Anonymous class listener

Source: Internet
Author: User

The Listener class is a listener object that is deliberately designed to create a GUI component. The listener is not shared by other applications, so the correct approach is to define him as an inner class in the Framework class.


Of course, you can use anonymous inner classes to simplify internal class listeners.

An anonymous inner class is an inner class that has no name.

He completes the definition of an inner class and creates an instance of the class.



Because the anonymous inner class is a special inner class, it can be thought of as an inner class with the following characteristics:

1. The anonymous inner class must always extend the parent class or implement the interface, but he cannot have the extends and implements clauses displayed.

2. Anonymous pinch not tired you must implement all the abstract methods in the parent class or interface.

3. Anonymous inner class always uses his Fred lunch constructor method to create an instance, if an anonymous inner class implements an interface, the constructor method is object ()


Anonymous inner class exercises:

Package Chapter16;import Java.awt.flowlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import javax.swing.jbutton;import javax.swing.JFrame; @SuppressWarnings ("Serial") public class Anonymouslistenerdemo extends jframe{private JButton stringnew = new JButton ("new");p rivate JButton Stringop En = new JButton ("open");p rivate JButton stringsave = new JButton ("Save");p rivate JButton stringPrint = new JButton ("Print ");p ublic Anonymouslistenerdemo () {setlayout (New FlowLayout ()); Add (stringnew); add (Stringopen); add (stringsave); add (StringPrint); Stringnew.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {System.out.println ("Process New");}); Stringopen.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) { System.out.println ("Process Open");}); Stringsave.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) { System.out.println ("Process Save");}); StringpRint.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {System.out.println ("Process Print");});} public static void Main (string[] args) {Anonymouslistenerdemo frame = new Anonymouslistenerdemo (); Frame.settitle ("Anonymous inner class "); Frame.setvisible (true); Frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.setlocationrelativeto ( NULL); Frame.pack ();}}




"Fundamentals of Java language Programming"--event-driven programming--Anonymous class 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.