Java Learning: Notes for AWT components and event handling (1)--ActionEvent events on text boxes

Source: Internet
Author: User
Tags gettext

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

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.