Java Event handling 4 (Focus, keyboard

Source: Internet
Author: User

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

Related Article

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.