CSDN博客

img evilzydar

TextField中进行字符的输入限制

发表于2004/9/20 15:09:00  597人阅读

1)java.awt.TextField 版本,我实在是想不出其他办法, 这个办法是比较低劣的:

public void setInputLimited(int min , int max  TextField text){

  t.addKeyListener(new java.awt.event.KeyAdapter (){
             public void keyPressed(KeyEvent e) {
                 if(e.getKeyCode() >max||e.getKeyCode()                     t.setText(t.getText().substring(0,t.getText().length() -1) );
                 }
             }
        });

}

2)javax.swing.JTextField

JTextField和TextField不一样, 他有一个setDocument(Document doc)方法, 可以来设置输入。

import java.awt.*;
import javax.swing.text.*;

public void setInputLimited(int min , int max,JtextField text){

   text.setDocument(new NumberOnlyDocument());

}


public class NumberOnlyDocument
        extends PlainDocument {
    private boolean haveDot = false;
    private int length = 0;
    private double max = Double.MIN_VALUE, min = Double.MAX_VALUE;
  
    public NumberOnlyDocument(double max, double min) {
        if (max >= min) {
            this.max = max;
            this.min = min;
        }
       
    }

  
  

    public void insertString(int offs, String str, AttributeSet a) throws
            BadLocationException {

        if (str == null) {
            return;
        }
        char[] number = str.toCharArray();
        for (int i = 0; i < number.length; i++) {
            if (offs == 0) {
                if (! (number[i] >= '0' && number[i] <= '9' || number[i] == '.' ||
                        number[i] == '-' || number[i] == '+')) {
                    if (offs == length - 1) {
                        remove(offs + i, 1);
                    }
                    else {
                        return;
                    }

                }
                else {
                    length++;
                }
            }
            else {
                if (!haveDot) {
                    if (! (number[i] >= '0' && number[i] <= '9' || number[i] == '.')) {
                        if (offs == length - 1) {
                            remove(offs + i, 1);
                        }
                        else {
                            return;
                        }
                    }
                    else {
                        if (number[i] == '.') {
                            haveDot = true;
                        }
                        length++;
                    }
                }
                else {
                    if (! (number[i] >= '0' && number[i] <= '9')) {
                        if (offs == length - 1) {
                            remove(offs + i, 1);
                        }
                        else {
                            Toolkit.getDefaultToolkit().beep();
                            return;
                        }
                    }
                    else {
                        length++;
                    }
                }
            }
        }
      
        super.insertString(offs, new String(number), a);
    }
}

0 0

相关博文

我的热门文章

img
取 消
img