当前位置: 技术问答>java相关
如何设定jTextfield控件的输入长度?
来源: 互联网 发布时间:2015-01-08
本文导语: | 来,我告诉你,你用下面的方法,可心在里面设定只能输入数字,小数点位数,输入长度,都可以。 textfield.setDocument(new CustomTextFormator(max_length));能输入所有字符, setDocument(new CustomTextFormator(max_length-precis...
|
来,我告诉你,你用下面的方法,可心在里面设定只能输入数字,小数点位数,输入长度,都可以。
textfield.setDocument(new CustomTextFormator(max_length));能输入所有字符,
setDocument(new CustomTextFormator(max_length-precision, type(CustomTextFormator.INTEGER_TEXT,CustomTextFormator.FLOAT_TEXT,CustomTextFormator.DATE_YEAR), precision(小数位)));根据类型确定能输入的字符
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
public class CustomTextFormator extends PlainDocument
{ private String strAllowChar =null;
private int intMaxLength =0;
private int intDataType=0;
private int intPricesion=0;
public static final int INTEGER_TEXT=1;
public static final int FLOAT_TEXT=2;
public static final int DATE_YEAR=3;
public static final int DATE_MONTH=4;
//--- Construct Function
public CustomTextFormator(int intMaxLength)
{ super();
this.intMaxLength = intMaxLength;
}
//----
public CustomTextFormator(int intMaxLength, String strCharList)
{ super();
this.intMaxLength = intMaxLength;
this.strAllowChar = strCharList;
}
//----
public CustomTextFormator(int intMaxLength, int intDataType)
{ super();
this.intMaxLength = intMaxLength;
this.intDataType = intDataType;
if(intDataType == this.INTEGER_TEXT)
this.strAllowChar = "-1234567890";
else if(intDataType == this.DATE_YEAR)
this.strAllowChar = "1234567890";
else if(intDataType == this.DATE_MONTH)
this.strAllowChar = "1234567890";
else if(intDataType == this.FLOAT_TEXT)
this.strAllowChar = "-1234567890.";
}
//----
public CustomTextFormator(int intMaxLength, int intDataType, int intPricesion)
{ super();
this.intMaxLength = intMaxLength;
this.intDataType = intDataType;
if(intDataType == this.INTEGER_TEXT)
this.strAllowChar = "-1234567890";
else if(intDataType == this.FLOAT_TEXT)
this.strAllowChar = "-1234567890.";
this.intPricesion = intPricesion;
}
//==== Check Function Panel ===
public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException
{
if (!(s == null)) {
//-- Check Length
String strLastText = super.getText(0,super.getLength());
if(this.intMaxLength>0)
{ /*if(s.length()>1)
{ if(s.getBytes().length+strLastText.getBytes().length>=(this.intMaxLength+this.intPricesion+1))
s=s.substring(0,this.intMaxLength-strLastText.getBytes().length);
}
else*/
{ int intIntegerPos = strLastText.indexOf(".");
if(intIntegerPos >=0)
{ if(intIntegerPos>this.intMaxLength)
return;
if(strLastText.getBytes().length>=(this.intMaxLength+this.intPricesion+1))
return;
}
else
{ if(strLastText.getBytes().length>=this.intMaxLength && (!s.equals(".")))
return;
}
}
}
//-- Check Char String--
if(this.strAllowChar instanceof String)
{ String strCheckChar=null;
for(int i=0;i0)
return;
//----
if(this.intDataType == this.DATE_MONTH)
{ if(strLastText.length()>0)
{ if(strLastText.compareTo("1")>0)
return;
else if(strLastText.compareTo("1")==0 && s.compareTo("2")>0)
return;
else if(strLastText.compareTo("0")==0 && s.compareTo("0")==0)
return;
}
}
//--- Check Precision -
else if(this.intPricesion >0)
{ int intDotPos=strLastText.lastIndexOf(".");
if(s.indexOf(".")>=0)
{ if(intDotPos >0)
return; //--- 已有点号
else //--- 插入点号
{ int intCurrentDotPos = s.indexOf(".");
if((s.length()-intCurrentDotPos-1) + (strLastText.length()-offset) > this.intPricesion)
return;
}
}
if(intDotPos>=0 && (strLastText.trim().length()-intDotPos>this.intPricesion)
&& offset>intDotPos)
return;
}
}
super.insertString(offset,s,attributeSet);
}
//============================
}
import java.io.*;
/**
A class for formatting numbers that follows printf conventions.
Also implements C-like atoi and atof functions
@version 1.20 25 Mar 1998
@author Cay Horstmann
*/
public class Format
{ /**
Formats the number following printf conventions.
Main limitation: Can only handle one format parameter at a time
Use multiple Format objects to format more than one number
@param s the format string following printf conventions
The string has a prefix, a format code and a suffix. The prefix and suffix
become part of the formatted output. The format code directs the
formatting of the (single) parameter to be formatted. The code has the
following structure
@exception IllegalArgumentException if bad format
*/
public Format(String s)
{ width = 0;
precision = -1;
pre = "";
post = "";
leading_zeroes = false;
show_plus = false;
alternate = false;
show_space = false;
left_align = false;
fmt = ' ';
int state = 0;
int length = s.length();
int parse_state = 0;
// 0 = prefix, 1 = flags, 2 = width, 3 = precision,
// 4 = format, 5 = end
int i = 0;
while (parse_state == 0)
{ if (i >= length) parse_state = 5;
else if (s.charAt(i) == '%')
{ if (i = length) parse_state = 5;
else if (s.charAt(i) == ' ') show_space = true;
else if (s.charAt(i) == '-') left_align = true;
else if (s.charAt(i) == '+') show_plus = true;
else if (s.charAt(i) == '0') leading_zeroes = true;
else if (s.charAt(i) == '#') alternate = true;
else { parse_state = 2; i--; }
i++;
}
while (parse_state == 2)
{ if (i >= length) parse_state = 5;
else if ('0'
textfield.setDocument(new CustomTextFormator(max_length));能输入所有字符,
setDocument(new CustomTextFormator(max_length-precision, type(CustomTextFormator.INTEGER_TEXT,CustomTextFormator.FLOAT_TEXT,CustomTextFormator.DATE_YEAR), precision(小数位)));根据类型确定能输入的字符
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
public class CustomTextFormator extends PlainDocument
{ private String strAllowChar =null;
private int intMaxLength =0;
private int intDataType=0;
private int intPricesion=0;
public static final int INTEGER_TEXT=1;
public static final int FLOAT_TEXT=2;
public static final int DATE_YEAR=3;
public static final int DATE_MONTH=4;
//--- Construct Function
public CustomTextFormator(int intMaxLength)
{ super();
this.intMaxLength = intMaxLength;
}
//----
public CustomTextFormator(int intMaxLength, String strCharList)
{ super();
this.intMaxLength = intMaxLength;
this.strAllowChar = strCharList;
}
//----
public CustomTextFormator(int intMaxLength, int intDataType)
{ super();
this.intMaxLength = intMaxLength;
this.intDataType = intDataType;
if(intDataType == this.INTEGER_TEXT)
this.strAllowChar = "-1234567890";
else if(intDataType == this.DATE_YEAR)
this.strAllowChar = "1234567890";
else if(intDataType == this.DATE_MONTH)
this.strAllowChar = "1234567890";
else if(intDataType == this.FLOAT_TEXT)
this.strAllowChar = "-1234567890.";
}
//----
public CustomTextFormator(int intMaxLength, int intDataType, int intPricesion)
{ super();
this.intMaxLength = intMaxLength;
this.intDataType = intDataType;
if(intDataType == this.INTEGER_TEXT)
this.strAllowChar = "-1234567890";
else if(intDataType == this.FLOAT_TEXT)
this.strAllowChar = "-1234567890.";
this.intPricesion = intPricesion;
}
//==== Check Function Panel ===
public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException
{
if (!(s == null)) {
//-- Check Length
String strLastText = super.getText(0,super.getLength());
if(this.intMaxLength>0)
{ /*if(s.length()>1)
{ if(s.getBytes().length+strLastText.getBytes().length>=(this.intMaxLength+this.intPricesion+1))
s=s.substring(0,this.intMaxLength-strLastText.getBytes().length);
}
else*/
{ int intIntegerPos = strLastText.indexOf(".");
if(intIntegerPos >=0)
{ if(intIntegerPos>this.intMaxLength)
return;
if(strLastText.getBytes().length>=(this.intMaxLength+this.intPricesion+1))
return;
}
else
{ if(strLastText.getBytes().length>=this.intMaxLength && (!s.equals(".")))
return;
}
}
}
//-- Check Char String--
if(this.strAllowChar instanceof String)
{ String strCheckChar=null;
for(int i=0;i0)
return;
//----
if(this.intDataType == this.DATE_MONTH)
{ if(strLastText.length()>0)
{ if(strLastText.compareTo("1")>0)
return;
else if(strLastText.compareTo("1")==0 && s.compareTo("2")>0)
return;
else if(strLastText.compareTo("0")==0 && s.compareTo("0")==0)
return;
}
}
//--- Check Precision -
else if(this.intPricesion >0)
{ int intDotPos=strLastText.lastIndexOf(".");
if(s.indexOf(".")>=0)
{ if(intDotPos >0)
return; //--- 已有点号
else //--- 插入点号
{ int intCurrentDotPos = s.indexOf(".");
if((s.length()-intCurrentDotPos-1) + (strLastText.length()-offset) > this.intPricesion)
return;
}
}
if(intDotPos>=0 && (strLastText.trim().length()-intDotPos>this.intPricesion)
&& offset>intDotPos)
return;
}
}
super.insertString(offset,s,attributeSet);
}
//============================
}
import java.io.*;
/**
A class for formatting numbers that follows printf conventions.
Also implements C-like atoi and atof functions
@version 1.20 25 Mar 1998
@author Cay Horstmann
*/
public class Format
{ /**
Formats the number following printf conventions.
Main limitation: Can only handle one format parameter at a time
Use multiple Format objects to format more than one number
@param s the format string following printf conventions
The string has a prefix, a format code and a suffix. The prefix and suffix
become part of the formatted output. The format code directs the
formatting of the (single) parameter to be formatted. The code has the
following structure
- a % (required)
- a modifier (optional)
+ forces display of + for positive numbers
0 show leading zeroes
- align left in the field
space prepend a space in front of positive numbers
# use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers. Don't suppress trailing zeroes in general floating point format.
- an integer denoting field width (optional)
- a period followed by an integer denoting precision (optional)
- a format descriptor (required)
f floating point number in fixed format
e, E floating point number in exponential notation (scientific format). The E format results in an uppercase E for the exponent (1.14130E+003), the e format in a lowercase e.
g, G floating point number in general format (fixed format for small numbers, exponential format for large numbers). Trailing zeroes are suppressed. The G format results in an uppercase E for the exponent (if any), the g format in a lowercase e.
d, i integer in decimal
x integer in hexadecimal
o integer in octal
s string
c character
@exception IllegalArgumentException if bad format
*/
public Format(String s)
{ width = 0;
precision = -1;
pre = "";
post = "";
leading_zeroes = false;
show_plus = false;
alternate = false;
show_space = false;
left_align = false;
fmt = ' ';
int state = 0;
int length = s.length();
int parse_state = 0;
// 0 = prefix, 1 = flags, 2 = width, 3 = precision,
// 4 = format, 5 = end
int i = 0;
while (parse_state == 0)
{ if (i >= length) parse_state = 5;
else if (s.charAt(i) == '%')
{ if (i = length) parse_state = 5;
else if (s.charAt(i) == ' ') show_space = true;
else if (s.charAt(i) == '-') left_align = true;
else if (s.charAt(i) == '+') show_plus = true;
else if (s.charAt(i) == '0') leading_zeroes = true;
else if (s.charAt(i) == '#') alternate = true;
else { parse_state = 2; i--; }
i++;
}
while (parse_state == 2)
{ if (i >= length) parse_state = 5;
else if ('0'
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。