Java syntax-listener

Source: Internet
Author: User

Four Methods for adding Java listeners (self, external class, internal class, and Anonymous class)

Import javax. Swing .*; 
Import java. AWT .*; 
Import java. AWT. event .*; 
     
Public class thisclassevent extends jframe implements actionlistener {   
   Public thisclassevent (){       
       Setlayout (New flowlayout ());          
       Jbutton BTN = new jbutton ("OK ");
       Add (BTN );
       BTN. addactionlistener (this ); 
   }   
   Public void actionreceivmed (actionevent e ){ 
   System. Out. println ("the OK button is clicked ");
   }   
   Public static void main (string ARGs []) {
   Thisclassevent frame = new thisclassevent ();
       Frame. settitle ("Its Class As event listener ");
       Frame. setlocationrelativeto (null); // center the frame
       Frame. setdefaclocloseoperation(Jframe. exit_on_close );
       Frame. setsize (280,100 );
       Frame. setvisible (true );
       New thisclassevent (); 
   } 
}

Import java. AWT .*; 
Import java. AWT. event .*; 
Import javax. Swing .*; 
    
Public class outerclassevent extends jframe {    
   Public outerclassevent (){ 
   Setlayout (New flowlayout ());          
       Jbutton BTN = new jbutton ("OK ");
       Add (BTN );
       Outerclass btlistener = new outerclass ();
       BTN. addactionlistener (btlistener ); 
   }   
   Public static void main (string ARGs []) { 
   Outerclassevent frame = new outerclassevent ();
       Frame. settitle ("external Class As event listener ");
       Frame. setlocationrelativeto (null); // center the frame
       Frame. setdefaclocloseoperation(Jframe. exit_on_close );
       Frame. setsize (280,100 );
       Frame. setvisible (true );
       New thisclassevent (); 
   } 
}    
Class outerclass implements actionlistener {
   Public void actionreceivmed (actionevent e ){ 
   System. Out. println ("the OK button is clicked ");
   } 
} 

Import java. AWT .*; 
Import java. AWT. event .*; 
Import javax. Swing .*; 
    
Class innerclassevent extends jframe {  
   Public innerclassevent (){ 
   Setlayout (New flowlayout ());          
       Jbutton BTN = new jbutton ("OK ");
       Add (BTN );
       Outerclass btlistener = new outerclass ();
       BTN. addactionlistener (btlistener ); 
   }
   Class innerclass implements actionlistener { 
       Public void actionreceivmed (actionevent e ){ 
       System. Out. println ("the OK button is clicked ");
       } 
   } 
   Public static void main (string ARGs []) { 
   Innerclassevent frame = new innerclassevent ();
       Frame. settitle ("internal class as event listener ");
       Frame. setlocationrelativeto (null); // center the frame
       Frame. setdefaclocloseoperation(Jframe. exit_on_close );
       Frame. setsize (280,100 );
       Frame. setvisible (true );
       New thisclassevent (); 
   } 
 
}

Import java. AWT .*; 
Import java. AWT. event .*; 
Import javax. Swing .*; 
       
Class anonymousevent extends jframe {  
   Public anonymousevent (){ 
   Setlayout (New flowlayout ());          
       Jbutton BTN = new jbutton ("OK ");
       Add (BTN );    
       BTN. addactionlistener ( 
           New actionlistener (){ 
               Public void actionreceivmed (actionevent e ){ 
               System. Out. println ("the OK button is clicked ");
               } 
           } 
       );  
   } 
   Public static void main (string ARGs []) { 
   Anonymousevent frame = new anonymousevent ();
       Frame. settitle ("anonymous internal class as event listener ");
       Frame. setlocationrelativeto (null); // center the frame
       Frame. setdefaclocloseoperation(Jframe. exit_on_close );
       Frame. setsize (280,100 );
       Frame. setvisible (true );
       New thisclassevent (); 
   } 
}  

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.