GUI Programming Note (Java) 09:gui control text box can only enter numeric character cases

Source: Internet
Author: User

1. First, let's look at my needs, as follows:

control text box can only enter numeric characters 2. Source code:
  1.  Packagecn.itcast_07;Importjava.awt.FlowLayout;ImportJava.awt.Frame;ImportJava.awt.Label;ImportJava.awt.TextField;ImportJava.awt.event.KeyAdapter;Importjava.awt.event.KeyEvent;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;/** If you enter a non-numeric character, the effect of your keyboard entry will be canceled. */ Public classFramedemo { Public Static voidMain (string[] args) {//Create a Form object and set propertiesFrame f =NewFrame ("Cannot enter non-numeric characters"); F.setbounds (400, 200, 400, 300); F.setlayout (NewFlowLayout ()); //Create a label label ObjectLabel label =NewLabel ("Please enter your QQ number, can not be a non-digital, do not believe you try"); TextField TF=NewTextField (40); //Add to formf.add (label);        F.add (TF); //set the form to closeF.addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {system.exit (0);        }        }); //add an event to a text boxTf.addkeylistener (NewKeyadapter () {@Override Public voidkeypressed (KeyEvent e) {//If you get a character that is not a numeric character, cancel the event//idea: First get character, judge character, cancel event//Char Getkeychar ()                CharCH =E.getkeychar ();//System.out.println (CH);                if(! (ch>= ' 0 ' && ch<= ' 9 ') ) {e.consume ();        }            }        }); //Set Form VisibleF.setvisible (true); }}




From for notes (Wiz)



GUI Programming Note (Java) 09:gui control text box can only enter numeric character cases

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.