Java page design-event processing Overview

Source: Internet
Author: User

When you have designed all the interfaces, you always need to add corresponding execution actions to the components. In JAVA, there is a corresponding time processing mechanism called "listener ", the process of adding corresponding execution actions to a component is called "Registration". The "listener" is an interface that contains the corresponding execution functions. You must implement the method functions by yourself, then "register" The component. In other words, my mom asked me to buy soy sauce. My mom didn't care how I would turn around the street for a few days and bargain with my boss and go home, the final result my mom needs is that I can deliver soy sauce to her, and the process of purchasing soy sauce in mid-term is realized by me, I am the "listener" in the event processing mechanism and accept the instructions from my mom. My mom asked me to buy soy sauce and gave me the "register" event, another perfect embodiment of JAVA Implementation and interface separation.

In JAVA, component events include ActionEvent, KeyEvent, FocusEvent, ComponentEvent, MouseEvent, and AdjustmentEvent. Each component supports all or part of the events, the corresponding event has a corresponding Listener to listen to the event and implement the interface method. The programmer must create an event class object and implement the functions in it, then register it with the corresponding component. The following code is used to demonstrate it:Copy codeThe Code is as follows: import javax. swing .*;
Import java. awt .*;
Import java. awt. event .*;
Public class ButtonDemo {
Private JFrame
Frame = new JFrame ("ButtonDemo ");
Private JButton
B1 = new JButton ("button 1 "),
B2 = new JButton ("button 2 ");
Private JTextField
Txt = new JTextField (10 );
// An anonymous class is used for button listening.
Private ActionListener bl = new ActionListener (){
// Implement Abstract Functions in the listener class
Public void actionreceivmed (ActionEvent e ){
String name = (JButton) e. getSource (). getText ();
Txt. setText (name );
}
};
Public ButtonDemo (){
// Register the listener object to two buttons
B1.addActionListener (bl );
B2.addActionListener (bl );
Frame. setLayout (new FlowLayout ());
Frame. add (b1 );
Frame. add (b2 );
Frame. add (txt );
Frame. setVisible (true );
Frame. setSize (200,150 );
}
Public static void main (String [] args ){
New ButtonDemo ();
}
}

If there are more than one function in the listener interface, and I only want to implement one of the functions, it is displayed that the program cannot run, because you must implement all the functions in the interface, the program can run only through compilation. How can this problem be solved? Obviously, the JAVA language designer has taken this into consideration. Therefore, a guy called "adapter" is provided, which implements all functions in the interface by default and inherits the "adapter" class, and overwrite the function you are interested in:Copy codeThe Code is as follows: class MyMouseListener extends MouseAdapter {
Public void mouseClicked (MouseEvent e ){
// Implement the action when you click the mouse
}
}

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.