1. Event object:
Generally inherited from the java.util.EventObject object, which is defined by the developer.
2. Event source:
It is the source of the trigger event. Different event sources will trigger different event types.
3. Event listener:
The event listener is responsible for listening to the events emitted by the event source. An event listener usually implements the identification interface java.util.EventListener.
The whole processing process is as follows, the event source can register the event listener object, and An event object can be sent to an event listener object. After the event occurs, the event source sends the event object to all registered event listeners. The
listener object will then respond to the event according to the corresponding method in the event object.
The following are four implementation methods
Java code:
package TT; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.RootPaneContainer; /** * Own class as event listener: */ class EventListener1 extends JFrame implements ActionListener { private JButton btBlue, btDialog; public EventListener1() { setTitle( "Java GUI event listener processing" ); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue" ); btDialog = new JButton("弹窗"); // Add the button to the event listener btBlue.addActionListener( this ); btDialog.addActionListener(this); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // ***************************事件处理*************************** @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btBlue) { Container c = getContentPane(); c.setBackground(Color.BLUE); } else if (e.getSource() == btDialog) { JDialog dialog = new JDialog(); dialog.setBounds ( 300, 200, 400, 300 ); dialog.setVisible(true); } } } /** * Java event listener processing - inner class processing * */ class EventListener2 extends JFrame { private JButton btBlue, btDialog; // Constructor public EventListener2() { setTitle( "Java GUI event listener processing" ); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue" ); btDialog = new JButton("pop-up window" ); // Add event listener object (object-oriented thinking) btBlue.addActionListener( new ColorEventListener()); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // Inner class ColorEventListener, implements ActionListener interface class ColorEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } } // Inner class DialogEventListener, implements ActionListener interface class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds ( 300, 200, 400, 300 ); dialog.setVisible(true); } } } /** * Java event listener processing - anonymous inner class processing * */ class EventListener3 extends JFrame { private JButton btBlue, btDialog; public EventListener3() { setTitle( "Java GUI event listener processing" ); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue" ); btDialog = new JButton("弹窗"); // Add event listener (anonymous class here) btBlue.addActionListener( new ActionListener() { // Event handling @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } }); // And add an event listener btDialog.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds ( 300, 200, 400, 300 ); dialog.setVisible(true); } }); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } /** * Java event listener processing - external class processing * */ class EventListener4 extends JFrame { private JButton btBlue, btDialog; public EventListener4() { setTitle( "Java GUI event listener processing" ); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue" ); btDialog = new JButton("pop-up window" ); // Add button event listener btBlue.addActionListener( new ColorEventListener( this )); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } // External class ColorEventListener, implements ActionListener interface class ColorEventListener implements ActionListener { private EventListener4 el; ColorEventListener(EventListener4 el) { this.el = el; } @Override public void actionPerformed(ActionEvent e) { Container c = el.getContentPane(); c.setBackground(Color.BLUE); } } // External class DialogEventListener, implements ActionListener interface class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds ( 300, 200, 400, 300 ); dialog.setVisible(true); } } public class ActionListenerTest { public static void main(String args[]) { new EventListener4(); } }