Java interface Programming (bottom)

Source: Internet
Author: User

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)

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.