熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

只能輸入文件名和只能輸入數字的文本框

2013-11-23 18:39:47  來源: Java核心技術 

  package combrowser;

  import javaawtToolkit;

  import javaioFile;

  import javautilStringTokenizer;

  import javaxswingJTextField;

  import javaxswingtextAttributeSet;

  import javaxswingtextBadLocationException;

  import javaxswingtextDocument;

  import javaxswingtextPlainDocument;

  /**

  * 只能輸入文件名的文本框

  * @author dl

  */

  public class JFileNameTextField extends javaxswingJTextField{

  public static void main(String[] args) {

  // TODO Autogenerated method stub

  //測試代碼

  /*JFrame frame = new JFrame(文本框的內容限制測試);

  framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  frameadd(new JFileNameTextField(true) BorderLayoutCENTER);

  frameadd(new JNumTextField( ) BorderLayoutNORTH);

  framesetSize( );

  framesetVisible(true);*/

  }

  /**

  *<br>方法說明初始化

  *<br>輸入參數isPath 是否可以輸入帶目錄的文件名

  *<br>返回類型

  */

  public JFileNameTextField(boolean isPath)   {

  super();

  FileNameDocument fd = (FileNameDocument)thisgetDocument();

  fdsetIsPath(isPath);

  }

  protected Document createDefaultModel()   {

  return new FileNameDocument(this);

  }

  }

  /**

  * 判斷輸入字符是否符合文件名規范的過濾實現

  * @author dl

  */

  class FileNameDocument extends PlainDocument {

  private boolean isPath = false; /**是否可以輸入帶目錄的文件名*/

  private JTextField parent = null; /**PlainDocument所在的文本框*/

  private static Toolkit toolkit = ToolkitgetDefaultToolkit();

  public FileNameDocument(JTextField field) {

  super();

  thisparent = field;

  }

  public void setIsPath(boolean isPath) {

  thisisPath = isPath;

  }

  public void insertString(int offset String text AttributeSet attributes)

  throws BadLocationException {

  //得到當前文本框的內容

  String strValue = parentgetText();

  strValue = strValuesubstring( offset) + text

  + strValuesubstring(offset strValuelength());

  //判斷得到的文本是否符合文件名的語法規范

  if (isPath) {

  if (!isNormallyPathString(strValue)) {

  toolkitbeep();

  return;

  }

  } else {

  if (!isNormallyNameString(strValue)) {

  toolkitbeep();

  return;

  }

  }

  superinsertString(offset text attributes);

  }

  /**

  *<br>方法說明判斷文件路徑是否合法

  *<br>輸入參數strName 文件路徑

  *<br>返回類型符合語法規則的文件路徑

  */

  public static boolean isNormallyPathString(String strName) {

  int pos = strNameindexOf(:);

  if (strNameindexOf(\\\\) != )

  return false;

  if (pos == ) {

  StringTokenizer st = new StringTokenizer(strName \\);

  while (sthasMoreTokens()) {

  String strTemp = stnextToken();

  if (!isNormallyNameString(strTemp)) {

  return false;

  }

  }

  } else {

  String strPath = strNamesubstring( pos);

  if (strPathlength() == ) {

  javalangCharacter fq = strPathtoLowerCase()charAt();

  javalangCharacter fq = strNametoLowerCase()charAt(pos);

  if (fq != :)

  return false;

  if (strNamelength() > pos + ) {

  javalangCharacter fq = strNametoLowerCase()charAt(

  pos + );

  if (fq != \\)

  return false;

  }

  if (fq >= c && fq <= z) {

  if (!new File(fq + :)exists())

  return false;

  }

  } else {

  return false;

  }

  StringTokenizer st = new StringTokenizer(strNamesubstring(pos +

  strNamelength()) \\);

  while (sthasMoreTokens()) {

  String strTemp = stnextToken();

  if (!isNormallyNameString(strTemp)) {

  return false;

  }

  }

  }

  return true;

  }

  /**

  *<br>方法說明判斷文件名是否合法

  *<br>輸入參數strName 文件名

  *<br>返回類型符合語法規則的文件名

  */

  public static boolean isNormallyNameString(String strName) {

  int pos = strNameindexOf(:\\);

  if (pos == ) {

  }

  String strText = \t\r\n\\/:*?\<>|^___FCKpd___quot;;

  for (int i = ; i < strNamelength(); ++i) {

  String ch = StringvalueOf(strNamecharAt(i));

  if (strTextindexOf(ch) != ) {

  return false;

  }

  }

  return true;

  }

  }

  package combrowser;

  import javaawtToolkit;

  import javaioFile;

  import javautilStringTokenizer;

  import javaxswingJTextField;

  import javaxswingtextAttributeSet;

  import javaxswingtextBadLocationException;

  import javaxswingtextDocument;

  import javaxswingtextPlainDocument;

  /**

  * 只能輸入文件名的文本框

  * @author dl

  */

  public class JFileNameTextField extends javaxswingJTextField{

  public static void main(String[] args) {

  // TODO Autogenerated method stub

  //測試代碼

  /*JFrame frame = new JFrame(文本框的內容限制測試);

  framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  frameadd(new JFileNameTextField(true) BorderLayoutCENTER);

  frameadd(new JNumTextField( ) BorderLayoutNORTH);

  framesetSize( );

  framesetVisible(true);*/

  }

  /**

  *<br>方法說明初始化

  *<br>輸入參數isPath 是否可以輸入帶目錄的文件名

  *<br>返回類型

  */

  public JFileNameTextField(boolean isPath)   {

  super();

  FileNameDocument fd = (FileNameDocument)thisgetDocument();

  fdsetIsPath(isPath);

  }

  protected Document createDefaultModel()   {

  return new FileNameDocument(this);

  }

  }

  /**

  * 判斷輸入字符是否符合文件名規范的過濾實現

  * @author dl

  */

  class FileNameDocument extends PlainDocument {

  private boolean isPath = false; /**是否可以輸入帶目錄的文件名*/

  private JTextField parent = null; /**PlainDocument所在的文本框*/

  private static Toolkit toolkit = ToolkitgetDefaultToolkit();

  public FileNameDocument(JTextField field) {

  super();

  thisparent = field;

  }

  public void setIsPath(boolean isPath) {

  thisisPath = isPath;

  }

  public void insertString(int offset String text AttributeSet attributes)

  throws BadLocationException {

  //得到當前文本框的內容

  String strValue = parentgetText();

  strValue = strValuesubstring( offset) + text

  + strValuesubstring(offset strValuelength());

  //判斷得到的文本是否符合文件名的語法規范

  if (isPath) {

  if (!isNormallyPathString(strValue)) {

  toolkitbeep();

  return;

  }

  } else {

  if (!isNormallyNameString(strValue)) {

  toolkitbeep();

  return;

  }

  }

  superinsertString(offset text attributes);

  }

  /**

  *<br>方法說明判斷文件路徑是否合法

  *<br>輸入參數strName 文件路徑

  *<br>返回類型符合語法規則的文件路徑

  */

  public static boolean isNormallyPathString(String strName) {

  int pos = strNameindexOf(:);

  if (strNameindexOf(\\\\) != )

  return false;

  if (pos == ) {

  StringTokenizer st = new StringTokenizer(strName \\);

  while (sthasMoreTokens()) {

  String strTemp = stnextToken();

  if (!isNormallyNameString(strTemp)) {

  return false;

  }

  }

  } else {

  String strPath = strNamesubstring( pos);

  if (strPathlength() == ) {

  javalangCharacter fq = strPathtoLowerCase()charAt();

  javalangCharacter fq = strNametoLowerCase()charAt(pos);

  if (fq != :)

  return false;

  if (strNamelength() > pos + ) {

  javalangCharacter fq = strNametoLowerCase()charAt(

  pos + );

  if (fq != \\)

  return false;

  }

  if (fq >= c && fq <= z) {

  if (!new File(fq + :)exists())

  return false;

  }

  } else {

  return false;

  }

  StringTokenizer st = new StringTokenizer(strNamesubstring(pos +

  strNamelength()) \\);

  while (sthasMoreTokens()) {

  String strTemp = stnextToken();

  if (!isNormallyNameString(strTemp)) {

  return false;

  }

  }

  }

  return true;

  }

  /**

  *<br>方法說明判斷文件名是否合法

  *<br>輸入參數strName 文件名

  *<br>返回類型符合語法規則的文件名

  */

  public static boolean isNormallyNameString(String strName) {

  int pos = strNameindexOf(:\\);

  if (pos == ) {

  }

  String strText = \t\r\n\\/:*?\<>|^___FCKpd___quot;;

  for (int i = ; i < strNamelength(); ++i) {

  String ch = StringvalueOf(strNamecharAt(i));

  if (strTextindexOf(ch) != ) {

  return false;

  }

  }

  return true;

  }

  }

  view plaincopy to clipboardprint?
    package combrowser;

  import javaawtToolkit;

  import javaxswingJTextField;

  import javaxswingtextAttributeSet;

  import javaxswingtextBadLocationException;

  import javaxswingtextDocument;

  import javaxswingtextPlainDocument;

  /**

  * 只能輸入數字的文本框

  * @author dl

  */

  public class JNumTextField extends javaxswingJTextField {

  /**

  *<br>方法說明初始化

  *<br>輸入參數min 允許輸入的數值的最小值; max 允許輸入的數值的最大值

  *<br>返回類型

  */

  public JNumTextField(int min int max) {

  super();

  NumericDocument nd = (NumericDocument) thisgetDocument();

  ndsetMax(max);

  ndsetMin(min);

  ndsetTextField(this);

  }

  protected Document createDefaultModel() {

  return new NumericDocument( );

  }

  }

  /**

  * 判斷輸入內容是否屬於允許范圍內的數值的過濾實現

  * @author dl

  */

  class NumericDocument extends PlainDocument {

  protected int maxDigits = ; /**允許輸入的數值的最大值*/

  protected int minDigits = ; /**允許輸入的數值的最小值*/

  protected JTextField parent = null; /**PlainDocument所在的文本框*/

  protected static Toolkit toolkit = ToolkitgetDefaultToolkit();

  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) {

  thisparent = parent;

  }

  public void insertString(int offset String text AttributeSet attributes)

  throws BadLocationException {

  //得到當前文本框的內容

  String strValue = parentgetText();

  strValue = strValuesubstring( offset) + text

  + strValuesubstring(offset strValuelength());

  //判斷輸入內容是否屬於允許范圍內的數值

  int value = ;

  try {

  value = IntegerparseInt(strValue);

  } catch (NumberFormatException ex) {

  toolkitbeep();

  return;

  }

  if (value > maxDigits || value < minDigits) {

  toolkitbeep();

  return;

  }

  superinsertString(offset text attributes);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25541.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.