Comparison of Three event registration methods in Java swing

Source: Internet
Author: User

 SwingYes currentlyJavaIs an indispensable window Tool Group for creating a graphical user interface (Gui)ProgramPowerful Tool.Java swingThe component automatically generates various events to respond to user behavior.JavaEncapsulate events into event classes and define an event listener for each event class. A component registers the event listener method, indicating that the component must respond to the specified event. In other words, we can register the listener to listen to events generated by the event source, so as to process the user behavior we need to process in the event processing program.

Java swingThe general steps for processing component events are as follows:

 

1. Create a new component.

2. Add the component to the corresponding panel.

3. register the listener to listen to events generated by the event Source

4. Define the event handling method.

Register events in two ways: one is to use one listener and multipleIfStatement to determine which component generates events. Second, multiple internal classes are used to respond to various events generated by different components, one is anonymous internal class, and the other is general internal class.

The following three methods are used to register events. To illustrate how the above three methods are used to handle events.

 

 

1. Multiple ifStatement to implement

In this way: we want to inheritActionlistenerInterface, and to implementActionreceivmedMethod. PassGetactioncommand()To obtain the event source.

 

 

 1   Public   Class Test_01 Extends Jframe Implements Actionlistener {  2   3   Test_01 (){  4 Jpanel = New  Jpanel ();  5 Jbutton button1 = New Jbutton ("button 1" );  6 Jbutton button2 = New Jbutton ("button 2" );  7  8   Panel. Add (button1 );  9   Panel. Add (button2 );  10           This  . Getcontentpane (). Add (panel );  11           This . Setvisible ( True  );  12           13 Button1.addactionlistener ( This  ); 14 Button2.addactionlistener ( This  );  15   16   }  17   18       Public   Void  Actionreceivmed (actionevent e ){  19 String source = E. getactioncommand ();  20           If (Source. Equals ("button 1")){  21 System. Out. println ("You clicked" );  22   }  23           If (Source. Equals ("button 2" )){  24 System. Out. println ("You pressed Button 2" );  25   }  26   }  27  28       Public   Static   Void  Main (string ARGs []) {  29           New  Test_01 ();  30   31   }  32 }

 

 

The disadvantage of using a listener to handle events is that:In fact, when the number of events to be processed is relatively small, this method is still a good method, it is simple. When the program is complex, a large stringIfStatement. ProgramCodeDifficult to read and maintain.

 

 

1. using anonymous internal classes

Using anonymous internal classes can solve the problem.IfTo obtain the event source. However, the use of anonymous internal classes also has some problems. Because it is associated with the event group. Depending on the location of the event group in the Code, the definition of the class and the processing of the event are also not easy to read. If the event processing program is complex, the code in the internal class will become very long.

 

 1   Public   Class Test_02 Extends  Jframe {  2       3   Test_02 (){  4 Jpanel =New  Jpanel ();  5 Jbutton button1 = New Jbutton ("button 1" );  6 Jbutton button2 = New Jbutton ("button 2" );  7   8   Panel. Add (button1 );  9   Panel. Add (button2 );  10           This . Getcontentpane (). Add (panel );  11           This . Setvisible ( True  );  12           13   Button1.addactionlistener (  14                   New  Actionlistener (){  15                       Public   Void  Actionreceivmed (actionevent e ){  16 System. Out. println ("You clicked" );  17   }  18   });  19   Button2.addactionlistener (  20                   New  Actionlistener (){  21                       Public   Void  Actionreceivmed (actionevent e ){  22 System. Out. println ("You pressed Button 2" );  23   }  24   });  25   }  26       27       Public   Static   Void  Main (string ARGs []) {  28           New  Test_02 (); 29   }  30 }

 

 

 

Iii. Use General Internal classes to implement

 1   Public   Class Test_03 Extends  Jframe {  2   3   Test_03 (){ 4 Jpanel = New  Jpanel ();  5 Jbutton button1 = New Jbutton ("button 1" );  6 Jbutton button2 = New Jbutton ("button 2" );  7   8   Panel. Add (button1 );  9   Panel. Add (button2 ); 10           This  . Getcontentpane (). Add (panel );  11           This . Setvisible ( True  );  12           13 Button1.addactionlistener ( New  Button1actionlistener ());  14 Button2.addactionlistener ( New  Button2actionlistener ()); 15           16   }  17       18       Private   Class Button1actionlistener Implements  Actionlistener {  19           Public   Void  Actionreceivmed (actionevent e ){  20 System. Out. println ("You clicked" ); 21   }  22   }  23       24       Private   Class Button2actionlistener Implements  Actionlistener {  25           Public   Void  Actionreceivmed (actionevent e ){  26 System. Out. println ("You pressed Button 2");  27   }  28   }  29       30       Public   Static   Void  Main (string [] ARGs ){  31           New  Test_03 ();  32   }  33  34 }

 

Using the general internal class, we can solve many problems. This method avoids code confusion caused by the use of anonymous internal classes in the second method. It puts all the event processing methods together and has meaningful names, so the program can be easily read and maintained. A single event handler can also be reused by the toolbar, menu bar, and so on.

 

Based on the above summary, we generally use the third method to register events.

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.