[Related information]
The Focus event class (FocusEvent) refers to a focus event that occurs when a component of the user program interface loses focus (that is, when the focus moves from one object to another).
Using focus events You must add an event handler for the Focuslistener interface to the component that contains the following two methods:
1) void focusgained (FocusEvent e): Occurs when the focus is obtained.
2) void Focuslost (FocusEvent e): Occurs when the focus is lost.
[Specific program Implementation]
PackageSup.orange.learn;Importjava.awt.*;Importjava.awt.event.*;/*** Created by Re-x on 10/28/14.*/ Public classFocuseventdemoextendsframe{TextArea TextArea; TextField TextField; PublicFocuseventdemo () {Super(); Init (); } Public Static voidMain (string[] args) {NewFocuseventdemo (); } Public voidinit () {setlayout (NewGridLayout (2, 1)); TextArea=NewTextArea (); Textarea.addfocuslistener (NewFocuslistener () {@Override Public voidfocusgained (focusevent e) {Textarea.settext ("Gained"); } @Override Public voidFocuslost (focusevent e) {Textarea.settext ("Lost"); } }); TextField=NewTextField (); Textfield.addfocuslistener (NewFocuslistener () {@Override Public voidfocusgained (focusevent e) {Textfield.settext ("TextField gained"); } @Override Public voidFocuslost (focusevent e) {Textfield.settext ("TextField Lost"); } }); Add (textarea); Add (TextField); Addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {Super. windowclosing (e); Dispose (); System.exit (0); } }); SetSize (200, 500); SetVisible (true); }}
Java interface-Focus event class