Java learning: AWT component and event processing notes (1) -- ActionEvent event on the text box, awtactionevent

Source: Internet
Author: User

Java learning: AWT component and event processing notes (1) -- ActionEvent event on the text box, awtactionevent

 

When learning to process events, you must have a good understanding of the event source, monitor, and interface for processing events.
1. Event Source
Objects that can generate java-recognized events can be called event sources, that is, the event source must be objects.
2. Monitor
Monitors event sources to process events
For example, for the text box, this method is:
AddActionListener (MONITOR );
3. event handling Interface
To enable the monitor object to process the event generated by the event source, the class of the monitor object must be declared to implement the corresponding interface, that is, the method bodies of all methods in this interface must be provided in the class body.
The java. awt. event package provides many event classes and interfaces for handling various events.
For the text box, the interface name is ActionListener, and the unique method of this interface is: public void actionreceivmed (ActionEvent e)
To monitor ActionEvent-type events, the event source must use the addActionListener method to obtain the monitor. The class that creates the monitor must implement the ActionListener interface.
The ActionEvent class has the following common methods:
1. public Object getSource ()
The ActionEvent object calls this method to obtain the reference of the event source object where an ActionEvent event occurs.
2. public String getActionCommand ()
The ActionEvent object can call this method to obtain a command string related to this event when an ActionEvent event occurs.
Note: The class of the monitor object must be declared to implement the corresponding interface:
Class A implements xxxListener
Practice: When you Enter an English word in the text box text1 and press Enter, the text box text3 immediately displays the meaning of Chinese. After you Enter a Chinese word in the text box text2 and press Enter, the text box text3 is immediately displayed

The Code is as follows:

Import java. awt. *; import java. awt. event. *; class Mywindow extends Frame implements ActionListener {TextField text1, text2, text3; Mywindow (String s) {setTitle (s); setLayout (new FlowLayout ()); text1 = new TextField (8); text2 = new TextField (8); text3 = new TextField (15); add (text1); add (text2); add (text3 ); text1.addActionListener (this); text2.addActionListener (this); setBounds (100,100,150,150); setVisible (true); validate ();} public void actionreceivmed (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 ("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 ("no word ");}}}} public class Example3 {public static void main (String [] args) {Mywindow win = new Mywindow (" ");}}

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.