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

📄 digitonly.java~1~

📁 银行系统中储蓄模块的实现
💻 JAVA~1~
字号:
package Error;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.Toolkit;
import java.util.regex.Pattern;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class DigitOnly extends PlainDocument{
    public DigitOnly() {
    }

    /*private static final long serialVersionUID = -4462693078138709956L;

    private String limit = null; // 输入字符限制的正则表达式

    private int maxLength = -1; // 输入字符最大长度的限制,-1为不限制

    private double maxValue = 0; // 如果输入的是数字,则最大值限制

    private boolean isMaxValue = false; // 是否采用了最大值限制

    private Toolkit toolkit = null; // 用来在错误的时候发出系统声音

    private boolean beep = false; // 是否发声,true为发出声音
    
    public DigitOnly() {
        super();
        this.init();
    }

    public DigitOnly(Content c) {
        super(c);
        this.init();
    }*/

    /**
     * 所有构造都需要的公共方法
     */
    /*private void init() {
        toolkit = Toolkit.getDefaultToolkit();
    }*/

    // 构造方法结束

    /**
     * 设置字符限制条件
     * 
     * @param limit
     * 限制条件 参考正则表达式 java.util.regex.Pattern
     */
    /*public void setCharLimit(String limit) {
        this.limit = limit;
    }*/

    /**
     * 返回字符限制的条件
     * 
     * @return 条件
     */
    /*public String getCharLimit() {
        return this.limit;
    }

    /**
     * 清除所有限制字符
     */
    /*public void clearLimit() {
        this.limit = null;
    }

    /**
     * 字符输入限制是否包含该字符
     * 
     * @param input
     * 字符
     * @return true为包含,false为不包含
     */
    /*public boolean isOfLimit(CharSequence input) {
        if (limit == null) {
            return true;
        } else {
            return Pattern.compile(limit).matcher(input).find();
        }
    }

    /**
     * 
     * 字符输入的限制组是否为空
     * 
     * @return true为空,false为有
     */
    /*public boolean isEmptyLimit() {
        if (limit == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置文本框所允许输入的最大字符长度
     * 
     * @param maxLength
     * 最大字符长度
     */
    /*public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    /**
     * 取消文本框最大字符长度的限制
     */
   /* public void cancelMaxLength() {
        this.maxLength = -1;
    }

    /**
     * 如果输入的为纯数字,则可用此方法来设置数字的最大值
     * 
     * @param maxValue
     * 最大值
     */
    /*public void setMaxValue(double maxValue) {
        this.isMaxValue = true;
        this.maxValue = maxValue;
    }

    /**
     * 文本框是否限制了数字内容的最大数值
     * 
     * @return true为限制了
     */
    /*public boolean isMaxValue() {
        return this.isMaxValue;
    }

    /**
     * 返回限制数字内容最大值
     * 
     * @return double类最大值,如果没有限制会返回0
     */
    /*public double getMaxValue() {
        return this.maxValue;
    }

    /**
     * 取消数字内容的最大值设置
     */
    /*public void cancelMaxValue() {
        this.isMaxValue = false;
        this.maxValue = 0;
    }

    /**
     * 使所有限制设置恢复默认
     */
    /*public void reset() {
        clearLimit();
        cancelMaxLength();
        cancelMaxValue();
    }

 

    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException, NumberFormatException {
        // 若字符串为空,直接返回。
        if (str == null) {
            return;
        }
        boolean b = true;
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            String temp = String.valueOf(ch[i]);
            // 如果要输入的字符不在允许范围内
            if (!isOfLimit(temp)) {
                b = false;
            }
            // 如果有字符长度限制,并且现在的字符长度已经大于或等于限制
            if (maxLength > -1 && this.getLength() >= maxLength) {
                b = false;
            }

        }
        // 如果内容设置了最大数字
        if(isMaxValue) {
            String s = this.getText(0, this.getLength()); // 文档中已有的字符
            s = s.substring(0, offs) + str + s.substring(offs, s.length());
            if (Double.parseDouble(s) > maxValue) {
                if (beep) {
                    //toolkit***ep(); // 发出声音
                    System.out.println("Beep!");
                }
                return;
            }
        }

        // 如果输入不合法
        if(!b){
            if (beep) {
                System.out.println("Beep!");
            }
            return;
        }
        super.insertString(offs, new String(ch), a);
    }*/
             
    public void insertString(int offs,String str,AttributeSet attr)throws BadLocationException{   
        if(str==null||str.trim().length()==0)
            return;   
        boolean insertflag = true;   
        StringBuffer sb = new StringBuffer(getText(0,getLength()));   
        sb.insert(offs,str);   
        try{
            Long.parseLong(sb.toString());
            //Integer.parseInt(sb.toString());     
        }catch(NumberFormatException ex){   
            insertflag = false;   
        }   
        if(insertflag) {
            super.insertString(offs,str,attr);
        }
    }      
}



   

⌨️ 快捷键说明

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