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