Several ways to add events to Java (reproduced in Codebrother's article)

Source: Internet
Author: User

/** * Java Event Listener processing--Self class implementation ActionListener interface, as Event listener * * @author codebrother */class EventListener1 extends JFrame implements A  Ctionlistener {private JButton btblue, Btdialog;    Public EventListener1 () {settitle ("Java GUI Event Listener processing");    SetBounds (100, 100, 500, 350);    SetLayout (New FlowLayout ());       Btblue = new JButton ("Blue");        Btdialog = new JButton ("Pop Window");    Add the button to the event listener Btblue.addactionlistener (this);    Btdialog.addactionlistener (this);    Add (Btblue);    Add (Btdialog);    SetVisible (TRUE);  Setdefaultcloseoperation (Jframe.exit_on_close);     }//*************************** event handling *************************** @Override public void actionperformed (ActionEvent e) {      if (e.getsource () = = Btblue) {Container c = Getcontentpane ();    C.setbackground (Color.Blue);      } else if (e.getsource () = = Btdialog) {jdialog dialog = new JDialog ();      Dialog.setbounds (300, 200, 400, 300);    Dialog.setvisible (TRUE); }}}/** * Java Event Listener Processing--Internal class processing * * @author CodebrotHer */class EventListener3 extends JFrame {private JButton btblue, Btdialog;    Construction method Public EventListener3 () {settitle ("Java GUI Event Listener processing");    SetBounds (100, 100, 500, 350);    SetLayout (New FlowLayout ());    Btblue = new JButton ("Blue");    Btdialog = new JButton ("Pop Window");    Add Event Listener object (Object oriented idea) Btblue.addactionlistener (new Coloreventlistener ());    Btdialog.addactionlistener (New Dialogeventlistener ());    Add (Btblue);    Add (Btdialog);    SetVisible (TRUE);  Setdefaultcloseoperation (Jframe.exit_on_close); }//Internal class Coloreventlistener, implementing ActionListener Interface class Coloreventlistener implements ActionListener {@Override Publi      c void actionperformed (ActionEvent e) {Container c = Getcontentpane ();    C.setbackground (Color.Blue);    }}//internal class Dialogeventlistener, implementing ActionListener Interface class Dialogeventlistener implements ActionListener {@Override      public void actionperformed (ActionEvent e) {jdialog dialog = new JDialog (); Dialog.setbounds (300, 200, 400, 300);    Dialog.setvisible (TRUE); }}}/** * Java Event Listener processing--Anonymous internal class processing * * @author codebrother */class EventListener2 extends JFrame {private JButton btblue, BT  Dialog;    Public EventListener2 () {settitle ("Java GUI Event Listener processing");    SetBounds (100, 100, 500, 350);    SetLayout (New FlowLayout ());    Btblue = new JButton ("Blue");        Btdialog = new JButton ("Pop Window"); Add Event Listener (anonymous class here) Btblue.addactionlistener (new ActionListener () {//Event handling @Override public void action        Performed (ActionEvent e) {Container c = Getcontentpane ();      C.setbackground (Color.Blue);        }    }); and add Event listener Btdialog.addactionlistener (new ActionListener () {@Override public void actionperformed (Actionev        Ent e) {jdialog dialog = new JDialog ();        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 classesDaniel * * @author codebrother */class EventListener4 extends JFrame {private JButton btblue, Btdialog;    Public EventListener4 () {settitle ("Java GUI Event Listener processing");    SetBounds (100, 100, 500, 350);    SetLayout (New FlowLayout ());    Btblue = new JButton ("Blue");    Btdialog = new JButton ("Pop Window");    Add the button to the event listener Btblue.addactionlistener (new Coloreventlistener (this));    Btdialog.addactionlistener (New Dialogeventlistener ());    Add (Btblue);    Add (Btdialog);    SetVisible (TRUE);  Setdefaultcloseoperation (Jframe.exit_on_close); }}//External class Coloreventlistener, implement ActionListener interface class Coloreventlistener implements ActionListener {private  EventListener4 el;  Coloreventlistener (EventListener4 el) {this.el = El;    } @Override public void actionperformed (ActionEvent e) {Container c = El.getcontentpane ();  C.setbackground (Color.Blue); }}//External class Dialogeventlistener, implementing ActionListener Interface class Dialogeventlistener implements ActionListener {@Override public void actionperformed (ActionEvent e) {JDialog dialog = new JDialog ();    Dialog.setbounds (300, 200, 400, 300);  Dialog.setvisible (TRUE);  }}public class actionlistenertest{public static void Main (String args[]) {new EventListener2 (); }}

Several ways to add events to Java (reprinted with Codebrother articles)

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.