When learning to handle events, you must have a good grasp of event sources, monitors, interfaces for handling events
1. Event Source
An object capable of generating a Java-approved event can be called an event source, which means that the event source must be an object
2. Monitor
Monitor event sources to handle events that occur
For example: For a text box, this method is:
addActionListener (monitor);
3. Interface for handling events
In order for the monitor object to handle events that occur at the event source, the class that created the monitor object must declare the corresponding interface, that is, the method body of all the methods in that interface must be given in the class body
Many event classes and interfaces for handling various events are available in the Java.awt.event package.
For text boxes, the name of this interface is ActionListener, the only method for this interface is: public void actionperformed (ActionEvent e)
In order to be able to monitor events of type ActionEvent, the event source must use the addActionListener method to obtain the monitor, and the class that created the monitor must implement the interface ActionListener
The ActionEvent class has the following common methods:
1. Public Object GetSource ()
The ActionEvent object calls this method to get a reference to the event source object where the ActionEvent event occurred
2. Public String Getactioncommand ()
The ActionEvent object calls this method to get a command string that is related to the event when the ActionEvent event occurs.
Note: The class that creates the monitor object must declare the corresponding interface to be implemented:
Class A implements Xxxlistener
Walkthrough: When the user enters the English word in the text box Text1 and presses ENTER key, the text box Text3 immediately displays the Chinese meaning; in the text box Text2 in the input Chinese word and presses the ENTER key, the text box Text3 immediately displays the English meaning
The code is as follows:
Importjava.awt.*;Importjava.awt.event.*;classMywindowextendsFrameImplementsactionlistener{TextField text1,text2,text3; Mywindow (String s) {settitle (s); SetLayout (NewFlowLayout ()); Text1=NewTextField (8); Text2=NewTextField (8); Text3=NewTextField (15); Add (Text1); Add (TEXT2); Add (TEXT3); Text1.addactionlistener ( This); Text2.addactionlistener ( This); SetBounds (100,100,150,150); SetVisible (true); Validate (); } Public voidactionperformed (ActionEvent e) {if(E.getsource () = =Text1) {String Word=Text1.gettext (); if(Word.equals ("Boy")) {Text3.settext (Boy); } Else if(Word.equals ("Girl") {Text3.settext (Girl); } Else if(Word.equals ("Sun") {Text3.settext (Sun); } Else{Text3.settext ("There is no such word."); } } Else if(E.getsource () = =Text2) {String Word=Text2.gettext (); if(Word.equals ("Boy") {Text3.settext ("Boy."); } Else if(Word.equals ("Girl") {Text3.settext ("Girl"); } Else if(Word.equals ("Sun") {Text3.settext ("Sun"); } Else{Text3.settext ("There is no such word."); } } }} Public classexample3{ Public Static voidMain (string[] args) {Mywindow win=NewMywindow ("Chinese-English translation")); }}
Java Learning: Notes for AWT components and event handling (1)--ActionEvent events on text boxes