Summary of event processing in JAVA

Source: Internet
Author: User
Tags java se

1. events are often used in java se desktop development. Components Without events are like components without human life. An event must have an event source. To complete a function, you must have an event handling method, and components and events must cooperate and coordinate with each other to complete the expected results. An event is an object that describes an event type. The component that generates an event is usually called an event source. To complete a function, you must use an event processing method.
2. Main steps for event handling:
 
1. First, a listener is generated to check whether an event source exists.
2. register the listener on the component with the event processing function to be implemented.
3. register the Listener component to generate the event source and return the event object to the listener.
4. A listener calls the corresponding method to process the event.
 
3. Three Methods for registering event listeners:
 
1. Implemented through interfaces
 
Package com. neusoft;
 
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import javax. swing. JButton;
Import javax. swing. JFrame;
 
Public class EventThrouthInterface extends JFrame implements ActionListener {
Private JButton jb;
 
Public EventThrouthInterface (){
Jb = new JButton ("OK ");
Jb. addActionListener (this );
This. add (jb );
This. setSize (300,200 );
This. setdefaclocloseoperation (3 );
This. setVisible (true );
 
}
 
Public void actionreceivmed (ActionEvent e ){
This. jb. setText ("Event Response ");
}
 
Public static void main (String [] args ){
New EventThrouthInterface ();
}
}
2. Event adapter. The event adapter and the anonymous internal class are basically the same, except that the adapter is a class provided in JAVA to simplify programming. This class implements the methods in the corresponding event interface by default. For example, the event adapter corresponding to the WindowListener interface is WindowAdapter, And the adapter interface corresponding to the KeyListener is KeyAdapter. In this way, using the adapter saves the trouble of implementing all abstract methods through the interface, it should be because all the abstract methods have been implemented in the adapter. We only need to rewrite the method we want to implement.
Package com. neusoft;
 
Import java. awt. event. MouseAdapter;
Import java. awt. event. MouseEvent;
Import javax. swing. JButton;
Import javax. swing. JFrame;
 
Public class EventThrouthAdapter extends JFrame {
Private JButton jb;
 
Public EventThrouthAdapter (){
Jb = new JButton ("OK ");
Jb. addMouseListener (new MouseAdapter (){
Public void mouseClicked (MouseEvent e ){
Jb. setText ("Event Response ");
}
});
This. add (jb );
This. setSize (300,200 );
This. setdefaclocloseoperation (3 );
This. setVisible (true );
 
}
 
Public static void main (String [] args ){
New EventThrouthAdapter ();
}
}
3. Anonymous internal class.
Package com. neusoft;
 
Import java. awt. event. MouseEvent;
Import java. awt. event. MouseListener;
 
Import javax. swing. JButton;
Import javax. swing. JFrame;
 
Public class EventThrouthAdapter extends JFrame {
Private JButton jb;
 
Public EventThrouthAdapter (){
Jb = new JButton ("OK ");
Jb. addMouseListener (new MouseListener (){
Public void mouseClicked (MouseEvent e ){
Jb. setText ("Event Response ");
}
 
Public void mouseEntered (MouseEvent e ){
}
 
Public void mouseExited (MouseEvent e ){
}
 
Public void mousePressed (MouseEvent e ){
}
 
Public void mouseReleased (MouseEvent e ){
}
});
This. add (jb );
This. setSize (300,200 );
This. setdefaclocloseoperation (3 );
This. setVisible (true );
 
}
 
Public static void main (String [] args ){
New EventThrouthAdapter ();
}
}
Author: "extreme scholar"

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.