Package COM. browser; import Java. AWT. toolkit; import Java. io. file; import Java. util. stringtokenizer; import javax. swing. jtextfield; import javax. swing. text. attributeset; import javax. swing. text. badlocationexception; import javax. swing. text. document; import javax. swing. text. plaindocument;/*** you can only enter the text box * @ author DL */public class jfilenametextfield extends javax. swing. jtextfield {public static void main (S Tring [] ARGs) {// todo auto-generated method stub // test code/* jframe frame = new jframe ("text box content limit test"); frame. setdefaclocloseoperation (jframe. exit_on_close); frame. add (New jfilenametextfield (true), borderlayout. center); frame. add (New jnumtextfield (1,999), borderlayout. north); frame. setsize (300,150); frame. setvisible (true); */}/*** <br> method Description: initialization * <br> input parameter: whether ispath can input a file name with a directory * <br> return type: */Public jfilenametext Field (Boolean ispath) {super (); filenamedocument FD = (filenamedocument) This. getdocument (); FD. setispath (ispath);} protected document createdefamodel model () {return New filenamedocument (this );}} /*** filtering implementation for determining whether the input characters comply with the file name specifications * @ author DL */class filenamedocument extends plaindocument {private Boolean ispath = false; /** whether the file name with directory can be entered */private jtextfield parent = NULL;/** text box in which plaindocument is located */private s Tatic toolkit = toolkit. getdefatooltoolkit (); Public filenamedocument (jtextfield field) {super (); this. parent = field;} public void setispath (Boolean ispath) {This. ispath = ispath;} public void insertstring (INT offset, string text, attributeset attributes) throws badlocationexception {// get the content of the current text box string strvalue = parent. gettext (); strvalue = strvalue. substring (0, offset) + TEXT + strvalue. substri Ng (offset, strvalue. Length (); // determines whether the obtained text complies with the syntax of the file name. If (ispath) {If (! Isnormallypathstring (strvalue) {toolkit. Beep (); Return ;}} else {If (! Isnormallynamestring (strvalue) {toolkit. beep (); Return ;}} super. insertstring (offset, text, attributes);}/*** <br> method Description: determines whether the file path is legal * <br> input parameter: strname file path * <br> return type: file path conforming to syntax rules */public static Boolean isnormallypathstring (string strname) {int Pos = strname. indexof (":"); If (strname. indexof ("////")! =-1) return false; If (Pos =-1) {stringtokenizer ST = new stringtokenizer (strname, "//"); While (St. hasmoretokens () {string strtemp = ST. nexttoken (); If (! Isnormallynamestring (strtemp) {return false ;}} else {string strpath = strname. substring (0, POS); If (strpath. length () = 1) {Java. lang. character FQ = strpath. tolowercase (). charat (0); Java. lang. character fq1 = strname. tolowercase (). charat (POS); If (fq1! = ':') Return false; If (strname. length ()> POS + 1) {Java. lang. character fq2 = strname. tolowercase (). charat (Pos + 1); If (fq2! = '//') Return false;} If (FQ> = 'C' & FQ <= 'Z') {If (! New file (FQ + ":"). exists () return false ;}} else {return false;} stringtokenizer ST = new stringtokenizer (strname. substring (Pos + 1, strname. length (), "//"); While (St. hasmoretokens () {string strtemp = ST. nexttoken (); If (! Isnormallynamestring (strtemp) {return false ;}} return true ;}/ *** <br> method Description: determines whether the file name is valid * <br> input parameter: strname file name * <br> return type: file name conforming to the syntax */public static Boolean isnormallynamestring (string strname) {int Pos = strname. indexof (": //"); If (Pos =-1) {} string strtext = "/T/R/N ///:*? /"<> | ^ ___ Fckpd ___ 0 quot; For (INT I = 0; I <strname. length (); ++ I) {string CH = string. valueof (strname. charat (I); If (strtext. indexof (CH )! =-1) {return false ;}} return true ;}}
Package COM. browser; import Java. AWT. toolkit; import javax. swing. jtextfield; import javax. swing. text. attributeset; import javax. swing. text. badlocationexception; import javax. swing. text. document; import javax. swing. text. plaindocument;/*** only enter the text box of numbers * @ author DL */public class jnumtextfield extends javax. swing. jtextfield {/*** <br> method Description: initialization * <br> input parameter: minimum value of the value allowed by min; max maximum allowed values * <br> return type: */Public jnumtextfield (INT min, int max) {super (); numericdocument nD = (numericdocument) This. getdocument (); nd. setmax (max); nd. setmin (min); nd. settextfield (this);} protected document createdefaultmodel () {return New numericdocument (0,100 );}} /*** filter whether the input content is within the permitted range of values * @ author DL */class numericdocument extends plaindocument {protected int maxdigits =-1; /** maximum allowed values */protected int mindigits =-1;/** minimum allowed values */protected jtextfield parent = NULL; /** text box where plaindocument is located */protected static toolkit = toolkit. getdefatooltoolkit (); Public numericdocument (INT min, int max) {super (); maxdigits = max; mindigits = min;} public void setmax (INT max) {maxdigits = max ;} public void setmin (INT min) {mindigits = min;} public void settextfield (jtextfield parent) {This. parent = parent;} public void insertstring (INT offset, string text, attributeset attributes) throws badlocationexception {// get the content of the current text box string strvalue = parent. gettext (); strvalue = strvalue. substring (0, offset) + TEXT + strvalue. substring (offset, strvalue. length (); // determines whether the input content belongs to the allowed value int value =-1; try {value = integer. parseint (strvalue);} catch (numberformatexception ex) {toolkit. beep (); return;} If (value> maxdigits | value <mindigits) {toolkit. beep (); return;} super. insertstring (offset, text, attributes );}}