Java Learning Note (47)-Event monitoring

Source: Internet
Author: User
Tags event listener

Implementation class for defining the ActionListener interface implements event monitoring
ImportJava.awt.Button;ImportJava.awt.Frame;ImportJava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowEvent;ImportJava.awt.event.WindowListener;/ * Event monitoring * / Public  class Test04 {     Public Static void Main(string[] args) {Frame frame=NewFrame ("My Forms"); Button btn=NewButton ("point me.");//For button binding event listener, that is, register listener        //btn.addactionlistener (New Buttonhandler ());Btn.addactionlistener (NewActionListener () { Public void actionperformed(ActionEvent e) {//Get text on the buttonString Cmd=e.getactioncommand (); System.out.println (CMD);//Click the button to pop up a new formFrame frm=NewFrame ("I was clicked to pop the drip"); Frm.setsize ( -, -); Frm.setlocationrelativeto (NULL); Frm.setvisible (true); }        });//For form-bound event snoopingFrame.addwindowlistener (NewWindowListener () {@Override             Public void windowopened(WindowEvent e) {System.out.println ("***windowopened"); }@Override             Public void windowiconified(WindowEvent e) {//TODO auto-generated method stub}@Override             Public void windowdeiconified(WindowEvent e) {//TODO auto-generated method stub}@Override             Public void windowdeactivated(WindowEvent e) {//TODO auto-generated method stub}@Override             Public void windowclosing(WindowEvent e) {System.out.println ("***windowclosing"); }@Override             Public void windowclosed(WindowEvent e) {//TODO auto-generated method stub}@Override             Public void windowactivated(WindowEvent e) {System.out.println ("***windowactivated");        }        });        Frame.add (BTN); Frame.setsize ( $, $); Frame.setlocationrelativeto (NULL); Frame.setvisible (true); }}//define the implementation class for the ActionListener interfaceClass Buttonhandler implements actionlistener{//When the ActionEvent event occurs, the event object is automatically generated, passed in as a parameter, and the Actinperformed method is called automatically    @Override     Public void actionperformed(ActionEvent e) {//Get text on the buttonString Cmd=e.getactioncommand (); System.out.println (CMD);//Click the button to pop up a new formFrame frm=NewFrame ("I was clicked to pop the drip"); Frm.setsize ( -, -); Frm.setlocationrelativeto (NULL); Frm.setvisible (true); }}
To bind multiple listeners to a component
ImportJava.awt.Button;ImportJava.awt.Color;ImportJava.awt.Frame;ImportJava.awt.TextField;ImportJava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.FocusEvent;ImportJava.awt.event.FocusListener;ImportJava.awt.event.KeyEvent;ImportJava.awt.event.KeyListener;ImportJava.awt.event.MouseEvent;ImportJava.awt.event.MouseListener;ImportJava.awt.event.TextEvent;ImportJava.awt.event.TextListener;/ * * Bind multiple listeners for one component */ Public  class Test05 {Frame frame =NewFrame ("My Forms"); Button BTN =NewButton ("point me."); TextField txt=NewTextField (Ten);intCount =0; Public Test05() {init (); } Public void Init() {//For button binding ActionEvent monitoringBtn.addactionlistener (NewActionListener () {@Override             Public void actionperformed(ActionEvent e) {System.out.println ("***actionperformed");                count++; Btn.setlabel (count+""); }        });//For button binding MouseEvent monitoringBtn.addmouselistener (NewMouseListener () {@Override             Public void mousereleased(MouseEvent e) {//system.out.println ("****mousereleased");}@Override             Public void mousepressed(MouseEvent e) {//system.out.println ("****mousepressed");}@Override             Public void mouseexited(MouseEvent e) {//system.out.println ("****mouseexited");}@Override             Public void mouseentered(MouseEvent e) {//system.out.println ("****mouseentered");}@Override             Public void mouseclicked(MouseEvent e) {System.out.println ("****mouseclicked"); }        });//Bind Listener for text boxTxt.addfocuslistener (NewFocuslistener () {@Override             Public void Focuslost(FocusEvent e) {System.out.println ("***focuslost");                TextField txtsource= (TextField) E.getsource ();            Txtsource.setbackground (Color.White); }@Override             Public void focusgained(FocusEvent e) {System.out.println ("***focusgained"); TextField txtsource= (TextField) E.getsource ();//Get Event sourceTxtsource.setbackground (Color.gray);        }        }); Txt.addtextlistener (NewTextListener () {@Override             Public void textvaluechanged(TextEvent e) {System.out.println ("text box is changed"+e.paramstring ());        }        }); Txt.addkeylistener (NewKeyListener () {@Override             Public void keytyped(KeyEvent e) {//TODO auto-generated method stub}@Override             Public void keyreleased(KeyEvent e) {//TODO auto-generated method stub}@Override             Public void keypressed(KeyEvent e) {System.out.println (E.getkeycode () +" "+e.getkeychar ());if(E.getkeycode () ==keyevent.vk_enter) {System.out.println ("haha, you pressed the carriage return, was found!" ");        }            }        }); Frame.add ("North", TXT);        Frame.add (BTN); Frame.setsize ( $, $); Frame.setlocationrelativeto (NULL); Frame.setvisible (true); } Public Static void Main(string[] args) {NewTest05 (); }}
How the listener is implemented
Import Java.awt.button;import Java.awt.frame;import Java.awt.event.actionevent;import Java.awt.event.ActionListener;/ * * Listener Implementation Method */ Public  class Test06 extends Frame implements ActionListener {Button btn=NewButton ("point me."); PublicTest06 (String title) {Super(title);    Init (); } Public voidInit () {//Mode one: Anonymous inner class        /*btn.addactionlistener (New ActionListener () {@Override public void actionperformed (ActionEvent            e) {System.out.println ("use Anonymous inner class mode"); }        });*/        //Mode two        //btn.addactionlistener (New MyHandler ());        //Way three        //btn.addactionlistener (this);        //Mode fourBtn.addactionlistener (NewMyHandler2 ());        Add (BTN); SetSize ( $, $); Setlocationrelativeto (NULL); SetVisible (true); }//Way Three: Container class@Override Public voidactionperformed (ActionEvent e) {System.out.println ("Use container class mode"); }//Mode four: member Inner class     class MyHandler2 implements ActionListener{         Public voidactionperformed (ActionEvent e) {System.out.println ("Use external class mode"); }    } Public Static voidMain (string[] args) {NewTest06 ("Form"); }}//Mode two: external class class MyHandler implements ActionListener {@Override Public voidactionperformed (ActionEvent e) {System.out.println ("Use external class mode"); }}

Java Learning Note (47)-Event monitoring

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.