"Fundamentals of Java language Programming"--event-driven programming--understanding listeners, registering, and handling events

Source: Internet
Author: User

Use of the listener for the first time


Listener: Is an instance of a listener interface and must be registered on the source object


Listener Implementation Listener Interface

Create listener registers to source object above

Registration method: Call Source.addactionlistener () to register




First step (70 lines): Create Enlargelistener class to implement ActionListener interface
Step Two (28 lines): New one listener, then register to button1
Step three (54 lines): Create a enlarge method to be called
Fourth step (74 lines): Overwrite the Actionperformed method in the interface once the operation is monitored to perform the next sentence called the Enlarge method
Fifth step (line 71): to allow actionperformed to access the Enlarge method, define the class as inner class


Package Chapter16;import Java.awt.borderlayout;import Java.awt.graphics;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import javax.swing.JPanel;@ Suppresswarnings ("Serial") public class ControlCircle1 extends JFrame {private Circle1 circle = new Circle1 (20);//must be placed outside the method So that the Enlargelistener class can access the variable circleprivate JButton button1 = new JButton ("enlarge");p rivate JButton button2 = new JButton (" Shrink ");p ublic ControlCircle1 () {JPanel P1 = new JPanel ();p 1.add (button1);p 1.add (button2); Add (Circle, Borderlayout.center); Add (P1, Borderlayout.south); Button1.addactionlistener (new Enlargelistener ());//Second step: New One listener , and then register to button1 on Button2.addactionlistener (New Shrinklistener ());} public static void Main (string[] args) {ControlCircle1 a = new ControlCircle1 (); A.setdefaultcloseoperation (Jframe.exit_ On_close); A.setlocationrelativeto (null); A.setvisible (true); A.settitle ("displayshow"); A.setsize (300, 300);} Class Circle1 extends JPanel {PRivate int radius = 5;public circle1 () {}public circle1 (int radius) {This.radius = radius;} public void enlarge () {///Third step: Create enlarge method to be called radius +=20;repaint ();} public void shrink () {radius-=5;repaint ();} protected void Paintcomponent (Graphics g) {super.paintcomponent (g); G.drawoval (GetWidth ()/2-radius, getheight ()/2-r adius,2 * radius, 2 * radius);}} Class Enlargelistener implements ActionListener {//First step: Create Enlargelistener Class Implementation ActionListener interface// Fifth step: In order for actionperformed to access the Enlarge method, define the class as Inner class@overridepublic void actionperformed (ActionEvent e) {// Fourth step: Overwrite the Actionperformed method in the interface once the operation is monitored to perform the next sentence called the Enlarge method Circle.enlarge ();//handling event}}class Shrinklistener implements actionlistener{@Overridepublic void actionperformed (ActionEvent e) {Circle.shrink ();}}}


The enlarge and shrink keys are to control the enlargement and reduction of the circle


"Fundamentals of Java language Programming"--event-driven programming--understanding listeners, registering, and handling events

Related Article

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.