Java Event handling

Source: Internet
Author: User

1.ActionEven Events

text boxes, buttons, menu items, password boxes, radio buttons can all go actionevent events

Use

addActionListener (ActionListener listen1)

To register the monitor

ActionListener was originally an interface, and we had to write a class (or other interface) to implement it the only way

actionperformed (ActionEvent e)

Here e is the event source to give the method a parameter, the ActionEvent class has two methods

 Public Object GetSource (); // returns a reference to a transformed object on the event source  Public String Getactioncommand ()// Returns a related "command" string, such as this actionevent is a text box, that is to return his text content

A code that counts the word count

classComponentextendsjframe{JTextField test1;    JButton button1;    JTextArea testArea1;    Jradiobutton Radiobutton1,radiobutton2;    Buttongroup group1;        Component () {init (); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    voidinit () {setlayout (NewFlowLayout ()); Test1=NewJTextField (8); Button1=NewJButton ("Huang"); TESTAREA1=NewJTextArea (8,24);        Add (test1);        Add (button1); Add (NewJScrollPane (TESTAREA1)); Alistener a=NewAlistener (TEST1,BUTTON1,TESTAREA1); Test1.addactionlistener (a);//import Java.awt.event.ActionListener required;Button1.addactionlistener (a); }}classAlistenerImplementsactionlistener{JTextField test1;    JButton button1;    JTextArea testArea1; Alistener (JTextField Test,jbutton button,jtextarea testarea) {test1=test; Button1=button; TESTAREA1=Testarea; }     Public voidactionperformed (ActionEvent e) {String a=Test1.gettext (); Testarea1.append (A+ "Length" +a.length ()); }}

Java Event handling

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.