⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jhexjtextfield.java

📁 实现串口手机短信发送程序很全面的源代码
💻 JAVA
字号:
package com.sunfruit.comm.swing.ui;

import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.PlainDocument;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class JHexJTextField extends JTextField {

    private int maxLen=2;

    public JHexJTextField() {
        this(2);
    }

    public JHexJTextField(int maxLen) {
        this.maxLen=maxLen;
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
    private void jbInit() throws Exception {
        this.setDocument(new NumberDocument(maxLen));
    }

    class NumberDocument extends PlainDocument {

        private int maxLen=0;

        public NumberDocument(int maxLen) {
           this.maxLen=maxLen;
        }
        /**
         *
         * @param offs int 是在JTextField的Text中文字的输入位置距离起始位置偏移量
         * @param str String 输入的文字
         * @param a AttributeSet
         * @throws BadLocationException
         */
        public void insertString(int offs, String str, AttributeSet a) throws
                BadLocationException {
            boolean bool=appstring(str);
            if(!bool)
            {
                Toolkit.getDefaultToolkit().beep();
            }
            else
            {

                if((getLength()+str.length())>maxLen)
                    Toolkit.getDefaultToolkit().beep();
                else
                    super.insertString(offs,str,a);
            }
        }

        private boolean appstring(String str1) {
            if (!str1.equals("")) {
                for (int i = 0; i < str1.length(); i++) {
                    char c = str1.charAt(i);
                    /**
                     * 允许 97-102 [a-f],65-70 [A-F],48-57 [0-9]
                     */
                    if (!((((int) c) >= 48 && ((int) c) <= 57) || (((int) c) >= 97 && ((int) c) <= 102) || (((int) c) >= 65 && ((int) c) <= 70))) {
                        return false;
                    }
                }
            }
            return true;
        }

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -