如何限定用户在我的文本框中只能输入数字?在线等待!!! 你可以自己做一个方法判断输入的是不是字符是不是>='0'和<='9',在insert时效验 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import javax.swing.text.*;import java.math.BigDecimal;public class NumberFormatDocument extends PlainDocument{ private int textLength = -1; private int scale = -1; public NumberFormatDocument() { init(); } public NumberFormatDocument(int len) { this.textLength = len; init(); } public NumberFormatDocument(int len, int scale) { if(len > 0) this.textLength = len; if(scale > 0) this.scale = scale; init(); } public void setLength(int len) { if(len > 0) this.textLength = len; init(); } public void setScale(int scale) { if(scale > 0) this.scale = scale; init(); } protected void init() { try { if(0 < scale) { String n = "0."; for(int i = 0; i < scale; i++) { n += "0"; } this.replace(0,this.getLength(),n,null); } else this.replace(0,this.getLength(),"0",null); } catch(Exception e) { } } public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null || "".equals(str)) { if (this.getLength() == 0) { str = "0"; if (scale > 0) { str += "."; for (int i = 0; i < scale; i++) { str += "0"; } } insertNumber(offs, str, a); } return; } insertNumber(offs, str, a); } private void insertNumber(int offs, String str, AttributeSet a) throws BadLocationException { String text = ""; int len = 0; char strFirstChar = str.charAt(0); if (0 == offs) { if (strFirstChar == '-' || strFirstChar == '+') { text = str.substring(1); } else if (strFirstChar == '.' && scale > 0) { str = "0" + str; if (str.length() > 1) { text = str; } } else { text = str; } len = text.length(); } else { text = this.getText(0, offs) + str + this.getText(offs, getLength() - offs); if (this.getText(0, 1).equals("-") || this.getText(0, 1).equals("+")) { len = text.length() - 1; } else { len = text.length(); } } int dot = text.indexOf('.'); if(dot > 0) { if ( -1 != textLength && dot > textLength) return; if(scale < 0 || (scale > 0 && len - dot - 1 > scale)) { return; } } else { if ( -1 != textLength && len > textLength) return; } if (!"".equals(text)) { try { new BigDecimal(text); } catch (Exception e) { return; } } super.insertString(offs, str, a); }} 使用正则表达式,不过我这没有判断数字的例子,你去google搜一下就有了。 不用这么复杂,用java.swing.JFormattedTextField,再配合正则表达式简单的实现类就可以了。很简单的,u can do it! <script language="javascript"><!--function check(){if(isNaN(DNo)){ document.form.DNo.focus(); alert("工号必须是数字,请重输!"); return false; }}//--></script><form method="post" onsubmit="return check()" name="form" ><table><ta><input type="text" name="DNo"></td></table></form> public static boolean isValid(String s) { try { Integer.parseInt(s); return true; } catch(NumberFormatException e) { return false; } } 动态向panel添加控件 在哪里能找到活下载到sun.nio和sun.io包的源代码啊? 请教javadoc的用法,请高手指教 关于鼠标的小问题。 请教一个struts2的问题 java 中调用 masm and link Graphics2D绘图之XOR模式的疑惑 Applet中添加圆形图片icon的jButton,在IE中被点击时圆形图片与矩形边框间呈灰色,怎么改掉? 怎样获得当前类路径的上一级路径? 谁有关于java的DOM的例子?? 应该如何进行MS SQL Server 2000的jdbc配置 怎么样运行?急!!
import java.math.BigDecimal;public class NumberFormatDocument
extends PlainDocument
{
private int textLength = -1; private int scale = -1; public NumberFormatDocument()
{
init();
} public NumberFormatDocument(int len)
{
this.textLength = len;
init();
} public NumberFormatDocument(int len, int scale)
{
if(len > 0)
this.textLength = len;
if(scale > 0)
this.scale = scale;
init();
} public void setLength(int len)
{
if(len > 0)
this.textLength = len;
init();
} public void setScale(int scale)
{
if(scale > 0)
this.scale = scale;
init();
} protected void init()
{
try
{
if(0 < scale)
{
String n = "0.";
for(int i = 0; i < scale; i++)
{
n += "0";
}
this.replace(0,this.getLength(),n,null);
}
else
this.replace(0,this.getLength(),"0",null);
}
catch(Exception e)
{ }
} public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null || "".equals(str))
{
if (this.getLength() == 0)
{
str = "0";
if (scale > 0)
{
str += ".";
for (int i = 0; i < scale; i++)
{
str += "0";
}
}
insertNumber(offs, str, a);
}
return;
}
insertNumber(offs, str, a);
} private void insertNumber(int offs, String str, AttributeSet a)
throws BadLocationException
{
String text = "";
int len = 0;
char strFirstChar = str.charAt(0);
if (0 == offs)
{
if (strFirstChar == '-' || strFirstChar == '+')
{
text = str.substring(1);
}
else if (strFirstChar == '.' && scale > 0)
{
str = "0" + str;
if (str.length() > 1)
{
text = str;
}
}
else
{
text = str;
}
len = text.length();
}
else
{
text = this.getText(0, offs) + str +
this.getText(offs, getLength() - offs);
if (this.getText(0, 1).equals("-") ||
this.getText(0, 1).equals("+"))
{
len = text.length() - 1;
}
else
{
len = text.length();
}
}
int dot = text.indexOf('.');
if(dot > 0)
{
if ( -1 != textLength && dot > textLength)
return;
if(scale < 0 || (scale > 0 && len - dot - 1 > scale))
{
return;
}
}
else
{
if ( -1 != textLength && len > textLength)
return;
} if (!"".equals(text))
{
try
{
new BigDecimal(text);
}
catch (Exception e)
{
return;
}
} super.insertString(offs, str, a);
}
}
很简单的,u can do it!
<!--
function check(){
if(isNaN(DNo)){
document.form.DNo.focus();
alert("工号必须是数字,请重输!");
return false;
}
}
//-->
</script>
<form method="post" onsubmit="return check()" name="form" >
<table>
<ta><input type="text" name="DNo"></td>
</table>
</form>
{
try
{
Integer.parseInt(s);
return true;
}
catch(NumberFormatException e)
{
return false;
}
}