只能輸入檔案名稱和只能輸入數位文字框

來源:互聯網
上載者:User
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;/** * 只能輸入檔案名稱的文字框 * @author dl */public class JFileNameTextField extends javax.swing.JTextField{public static void main(String[] args) {// TODO Auto-generated method stub//測試代碼/*JFrame frame = new JFrame("文字框的內容限制測試");frame.setDefaultCloseOperation(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>方法說明:初始化     *<br>輸入參數:isPath 是否可以輸入帶目錄的檔案名稱     *<br>傳回型別:     */public JFileNameTextField(boolean isPath)   {  super(); FileNameDocument fd = (FileNameDocument)this.getDocument();fd.setIsPath(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 = Toolkit.getDefaultToolkit();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 {//得到當前文字框的內容String strValue = parent.getText();strValue = strValue.substring(0, offset) + text+ strValue.substring(offset, strValue.length());//判斷得到的文本是否符合檔案名稱的文法規範if (isPath) {if (!isNormallyPathString(strValue)) {toolkit.beep();return;}} else {if (!isNormallyNameString(strValue)) {toolkit.beep();return;}}super.insertString(offset, text, attributes);}/**     *<br>方法說明:判斷檔案路徑是否合法     *<br>輸入參數:strName 檔案路徑     *<br>傳回型別:符合文法規則的檔案路徑     */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>方法說明:判斷檔案名稱是否合法     *<br>輸入參數:strName 檔案名稱     *<br>傳回型別:符合文法規則的檔案名稱     */public static boolean isNormallyNameString(String strName) {int pos = strName.indexOf("://");if (pos == -1) {}String strText = "/t/r/n///:*?/"<>|^___FCKpd___0quot;;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;/** * 只能輸入數位文字框 * @author dl */public class JNumTextField extends javax.swing.JTextField {/**     *<br>方法說明:初始化     *<br>輸入參數:min 允許輸入的數值的最小值; max 允許輸入的數值的最大值     *<br>傳回型別:     */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);}}/** * 判斷輸入內容是否屬於允許範圍內的數值的過濾實現 * @author dl */class NumericDocument extends PlainDocument {protected int maxDigits = -1; /**允許輸入的數值的最大值*/protected int minDigits = -1; /**允許輸入的數值的最小值*/protected JTextField parent = null; /**PlainDocument所在的文字框*/protected static Toolkit toolkit = Toolkit.getDefaultToolkit();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 {//得到當前文字框的內容String strValue = parent.getText();strValue = strValue.substring(0, offset) + text+ strValue.substring(offset, strValue.length());//判斷輸入內容是否屬於允許範圍內的數值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);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.