Java Add Event listener four ways code instance _java

Source: Internet
Author: User

Several ways to add events to Java (reprinted the Codebrother article, making a slight change):

/** * Java Event Listener processing-Self class implementation ActionListener interface, as Event listener * * @author Codebrother/class EventListener1 extends JFrame Implem

  Ents ActionListener {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 ("flick 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, BTDI

  Alog;
    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 ("flick Window");
    Add Event Listener object (object-oriented thinking) Btblue.addactionlistener (new Coloreventlistener ());

    Btdialog.addactionlistener (New Dialogeventlistener ());
    Add (Btblue);
    Add (Btdialog);
    SetVisible (TRUE);
  Setdefaultcloseoperation (Jframe.exit_on_close); }//Internal class Coloreventlistener, implement ActionListener interface class Coloreventlistener implements ActionListener {@Override P
      ublic void actionperformed (ActionEvent e) {Container c = Getcontentpane ();
    C.setbackground (Color.Blue); 
    }//Internal class Dialogeventlistener, implement 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 inner class processing * @author Codebrother/class EventListener2 extends JFrame {private JButton

  Btblue, Btdialog;
    Public EventListener2 () {settitle ("Java GUI Event Listener processing");
    SetBounds (100, 100, 500, 350);

    SetLayout (New FlowLayout ());
    Btblue = new JButton ("Blue");
    
    Btdialog = new JButton ("flick Window"); Add Event Listener (here is anonymous Class) Btblue.addactionlistener (new ActionListener () {//Event handling @Override public void AC
        Tionperformed (ActionEvent e) {Container c = Getcontentpane ();
      C.setbackground (Color.Blue);
    
    }
    }); and add Event listener Btdialog.addactionlistener (new ActionListener () {@Override public void actionperformed (actio
        Nevent 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 class processing * * @author Codebrother/class EventListener4 extends JFrame {private JButton Btblu

  E, Btdialog;
    Public EventListener4 () {settitle ("Java GUI Event Listener processing");
    SetBounds (100, 100, 500, 350);
    SetLayout (New FlowLayout ());
    Btblue = new JButton ("Blue");
    Btdialog = new JButton ("flick Window");
    Add the button to the event listener Btblue.addactionlistener (this) (new Coloreventlistener);

    Btdialog.addactionlistener (New Dialogeventlistener ());
    Add (Btblue);
    Add (Btdialog);
    SetVisible (TRUE);
  Setdefaultcloseoperation (Jframe.exit_on_close); }//External class Coloreventlistener, implementing ActionListener Interface class Coloreventlistener implements ActionListener {private eventliste
  Ner4 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 Publi
    c 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 ();

 }
}

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.