First, event monitoring
Package Com.lost.actionevent;import Java.awt.borderlayout;import Java.awt.button;import java.awt.Frame;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;class Monitor2 implements actionlistener{public void actionperformed (ActionEvent e) { SYSTEM.OUT.PRINTLN ("a button has been pressed," + "the relative info is:\n" +e.getactioncommand ()); /* Use the returned Listener object E to call the Getactioncommand () method to get two buttons after the click Command to perform the return information according to the different extents of the returned information to separate which button is currently operating, BTN1 does not use the Setactioncommand () method setting The message returned by BTN1 is the text displayed on the button */}}public class ActionEventDemo2 {public static void main (string[] args) {//TODO auto-generated Method Stubframe F = new Frame ("ActionEventDemo2"); Button btn1 = New button ("Start"); Button btn2 = New button ("Stop"); Monitor2 m2 = new Monitor2 () Btn1.addactionlistener (m2); Btn1.setactioncommand ("Game Start ()"); Btn2.addactionlistener (m2); F.add (Btn1,borderlayout.north); F.add (Btn2,borderlayout.center); F.pack (); f. setvisible (True); Addwindowclosingevent (f);} private static void Addwindowclosingevent (Frame f) {F.addwindowlistener (new Windowadapter () {@Overridepublic void Windowclosing (WindowEvent e) {//TODO auto-generated method Stubsuper.windowclosing (e); System.exit (0);}});}}
Add a listener to the componet and set the listening action.
Second, TextField event monitoring
The TextField object may have an action (the cursor will hit enter in the text box) event, and the event class corresponding to the event is java.awt.event.ActionEvent.
A class object that implements the Java.awt.event.ActionListener interface for handling ActionEvent events. The ActionListener interface defines a method:
public void actionperformed (ActionEvent e)
The class that implements the interface to add a processing event (Action) statement to the method
Registers a ActionListener object with the addActionListener (Actonlistener L) method for the TextField object, and when the TextField object has an action event, Generates a ActionEvent object, which is passed as a parameter to the ActionListener object's Actionperformer method, in which the object's information can be obtained and processed accordingly.
Java interface Programming (bottom)