FocusEvent Focus Event
Interface
Addfocuslistener (Focuslistener Listener)
There are two ways of
Public void Focusgains (focusevent e) Public void focuslost (focusevent e)
Test code
classMywinextendsjframe{JTextField text1,text2; JButton Button1,button2; Mywin () {init (); SetVisible (true); Setdefaultcloseoperation (Jframe.exit_on_close); } voidinit () {Text1=NewJTextField (8); Add (Text1); SetLayout (NewFlowLayout ()); Focuspolice FocusPolice1=NewFocuspolice (); Text1.addfocuslistener (FocusPolice1); Add (NewJButton ("click")); }}classFocuspoliceImplementsfocuslistener{ Public voidfocusgained (focusevent e) {System.out.print ("11"); } Public voidFocuslost (focusevent e) {System.out.print ("22"); }}
Keyboard events
Addkeylistener (KeyEvent e)
KeyListener has three interfaces
void keypressed (keyevent e)// Press the keyboard void keyreleased (keyevent e)// Release keyboard void keytyped (keyevent e)// set of Actions
There are two methods of KeyEvent
getKeyCode ()// return a key code value, but do not know I always return 0Getkeychar ()// return the character on the key
A code that automatically jumps the text box
classMywinextendsjframe{JTextField text[]=NewJtextfield[3]; JButton Button1,button2; Mywin () {init (); SetVisible (true); Setdefaultcloseoperation (Jframe.exit_on_close); } voidinit () {setlayout (NewFlowLayout ()); Keypolice KeyPolice1=NewKeypolice (); for(inti=0;i<3;i++) {Text[i]=NewJTextField (8); Text[i].addkeylistener (KeyPolice1); Text[i].addfocuslistener (KeyPolice1); Add (Text[i]); } text[1].requestfocusinwindow (); Add (button1=NewJButton ("click")); }}classKeypoliceImplementskeylistener,focuslistener{ Public voidkeypressed (KeyEvent e) {} Public voidkeyreleased (KeyEvent e) {} Public voidkeytyped (KeyEvent e) {JTextField Text1=(JTextField) E.getsource (); if(Text1.gettext (). Length () >=6)//There's 7 of them to jump.Text1.transferfocus ();//Jump Function } Public voidfocusgained (focusevent e) {//JTextField text= (JTextField) E.getsource ();//It doesn't seem to work .//text.settext (null); } Public voidFocuslost (FocusEvent e) {}}
Java Event handling 4 (Focus, keyboard