Four ways to implement Java time monitoring

Source: Internet
Author: User
Tags event listener

1. Event object:
Typically inherits from Java.util.EventObject objects, which are defined by the developer themselves.

2. Event Source:
Is the source of the triggering event, and different event sources trigger different event types.

3. Event Listener:
The event listener is responsible for listening for events emitted by the event source. An event listener typically implements Java.util.EventListener this identity interface.

The entire process is such that the event source can register the event listener object and can send an event object to the event listener object. After the event occurs, the event source sends the event object to all the event listeners that have been registered.
The listener object then responds to the event based on the corresponding method within the event object.

Here are four ways to implement

Java code:

 PackageTT;ImportJava.awt.Color;ImportJava.awt.Container;Importjava.awt.FlowLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJavax.swing.JButton;ImportJavax.swing.JDialog;ImportJavax.swing.JFrame;ImportJavax.swing.RootPaneContainer;/*** Self class as Event listener:*/classEventListener1extendsJFrameImplementsActionListener {PrivateJButton Btblue, Btdialog;  PublicEventListener1 () {Settitle ("Java GUI Event Listener Processing"); SetBounds (100, 100, 500, 350); SetLayout (NewFlowLayout ()); Btblue=NewJButton ("Blue"); Btdialog=NewJButton ("Pop window"); //Add a button to an event listenerBtblue.addactionlistener ( This); Btdialog.addactionlistener ( This);        Add (Btblue);        Add (Btdialog); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    //*************************** Event Handling ***************************@Override Public voidactionperformed (ActionEvent e) {if(E.getsource () = =btblue) {Container C=Getcontentpane ();        C.setbackground (Color.Blue); } Else if(E.getsource () = =Btdialog) {JDialog Dialog=NewJDialog (); Dialog.setbounds (300, 200, 400, 300); Dialog.setvisible (true); }    }}/*** Java Event Listener Processing--internal class processing **/classEventListener2extendsJFrame {PrivateJButton Btblue, Btdialog; //Construction Method     PublicEventListener2 () {Settitle ("Java GUI Event Listener Processing"); SetBounds (100, 100, 500, 350); SetLayout (NewFlowLayout ()); Btblue=NewJButton ("Blue"); Btdialog=NewJButton ("Pop window"); //adding Event listener objects (object oriented thinking)Btblue.addactionlistener (NewColoreventlistener ()); Btdialog.addactionlistener (NewDialogeventlistener ());        Add (Btblue);        Add (Btdialog); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    //internal class Coloreventlistener, implement ActionListener interface    classColoreventlistenerImplementsActionListener {@Override Public voidactionperformed (ActionEvent e) {Container C=Getcontentpane ();        C.setbackground (Color.Blue); }    }    //internal class Dialogeventlistener, implement ActionListener interface    classDialogeventlistenerImplementsActionListener {@Override Public voidactionperformed (ActionEvent e) {JDialog dialog=NewJDialog (); Dialog.setbounds (300, 200, 400, 300); Dialog.setvisible (true); }    }}/*** Java Event Listener processing--Anonymous internal class processing **/classEventListener3extendsJFrame {PrivateJButton Btblue, Btdialog;  PublicEventListener3 () {Settitle ("Java GUI Event Listener Processing"); SetBounds (100, 100, 500, 350); SetLayout (NewFlowLayout ()); Btblue=NewJButton ("Blue"); Btdialog=NewJButton ("Pop window"); //Add an event listener (anonymous class here)Btblue.addactionlistener (NewActionListener () {//Event Handling@Override Public voidactionperformed (ActionEvent e) {Container C=Getcontentpane ();            C.setbackground (Color.Blue);        }        }); //and Add event listenersBtdialog.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {JDialog dialog=NewJDialog (); Dialog.setbounds (300, 200, 400, 300); Dialog.setvisible (true);        }        });        Add (Btblue);        Add (Btdialog); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }}/*** Java Event Listener Processing--External class processing **/classEventListener4extendsJFrame {PrivateJButton Btblue, Btdialog;  PublicEventListener4 () {Settitle ("Java GUI Event Listener Processing"); SetBounds (100, 100, 500, 350); SetLayout (NewFlowLayout ()); Btblue=NewJButton ("Blue"); Btdialog=NewJButton ("Pop window"); //Add a button to an event listenerBtblue.addactionlistener (NewColoreventlistener ( This)); Btdialog.addactionlistener (NewDialogeventlistener ());        Add (Btblue);        Add (Btdialog); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }}//External class Coloreventlistener, implement ActionListener interfaceclassColoreventlistenerImplementsActionListener {PrivateEventListener4 el; Coloreventlistener (EventListener4 el) { This. El =el; } @Override Public voidactionperformed (ActionEvent e) {Container C=El.getcontentpane ();    C.setbackground (Color.Blue); }}//External class Dialogeventlistener, implement ActionListener interfaceclassDialogeventlistenerImplementsActionListener {@Override Public voidactionperformed (ActionEvent e) {JDialog dialog=NewJDialog (); Dialog.setbounds (300, 200, 400, 300); Dialog.setvisible (true); }} Public classActionlistenertest { Public Static voidMain (String args[]) {NewEventListener4 (); }}

Four ways to implement Java time monitoring

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.