请教:JTextField边输入边验证,如验证无效,清空JTextField 两个文本框的内容相乘写入第三个文本框,对其是否数字格式进行验证。如验证无效,清空对应文本框的内容。我给JTextField注册了DocumentListener监听器。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DocumentListener有另外的用处吧 约束只要继承并覆盖就可以了static class IntegerDocument extends PlainDocument { public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { return; } int length = 0; char[] upper = str.toCharArray(); for (int i = 0; i < upper.length; i++) { if (upper[i] >= '0' && upper[i] <= '9') { upper[length++] = upper[i]; } } super.insertString(offs, new String(upper, 0, length), a); } } 监听JTextFiled的Document的事件, 对每次输入都验证, 进行相应的操作. 像lz这种需求 只要对结果框进行了约束就可以了 修改并测试过了static class IntegerDocument extends PlainDocument { public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { return; } int length = 0; char[] upper = str.toCharArray(); for (int i = 0; i < upper.length; i++) { if (upper[i] >= '0' && upper[i] <= '9') { upper[length++] = upper[i]; } if (upper[i] < '0' || upper[i] > '9') { length = 0; break; } } super.insertString(offs, new String(upper, 0, length), a); } }粘贴 打印都可以限制比如复制12ab34 就不会有任何效果 我的源代码是这样的: private class ResultListener implements DocumentListener { public void insertUpdate(DocumentEvent e) { getAnswer(); } public void removeUpdate(DocumentEvent e) { getAnswer(); } public void changedUpdate(DocumentEvent e) { } } //获取单价和数量文本域的内容,进行求积计算,并写入总价文本域中 public void getAnswer() { String nu=this.number.getText().trim(); String pr=this.pric.getText().trim(); if(!nu.equals("")&&!pr.equals("")){ if(nu.matches("^\\d+$")&&pr.matches("^\\d+[\\.]?\\d{0,2}$")){ try { int add1; double add2,answer; //获取第一个和第二个文本域的内容,并将其解析为数字类型变量 add1 = Integer.parseInt(nu); add2 = Double.parseDouble(pr); //求积 answer = add1 * add2; DecimalFormat df2 = new DecimalFormat("###.00"); //将结果写入第三个文本域中 this.sumpric.setText(df2.format(answer)); //对于解析错误的情况,将抛出异常 }catch (NumberFormatException e) { JOptionPane.showMessageDialog(this,"请输入数字格式!","提示!",JOptionPane.INFORMATION_MESSAGE); } }else{ try { if(!nu.matches("^\\d+$")){ JOptionPane.showMessageDialog(this,"请输入数字格式!","提示!",JOptionPane.INFORMATION_MESSAGE); //执行下面这一语句总是抛出IllegalStateException异常 this.number.setText("0"); } if(!pr.matches("^\\d+[\\.]?\\d{0,2}$")){ JOptionPane.showMessageDialog(this,"请输入数字格式!(最多两位小数)","提示!",JOptionPane.INFORMATION_MESSAGE); //执行下面这一语句总是抛出IllegalStateException异常 this.pric.setText("0.00"); } }catch (IllegalStateException e) { System.out.println("已经捕捉到IllegalStateException异常"); } this.sumpric.setText("0.00"); } } } 关于线程实现:如果等待10秒拿不到另一个资源,放弃当前资源 JTable问题 用SWING实现类似微软的IP输入框的问题 请懂得java和c#两门语言的朋友进来看一下. 关于netbeans和eclipse的swing的问题! Java: 这种菜单选择 怎么处理? 小问题 请问谁有jbuilder9的注册机?谢谢! 看来这个论坛也是徒有虚名,我的JB7编译成可执行文件的问题到现在没人能解决,难道这就是专家的水平? 控件的定位/鼠標指針隱藏(Applet) 有关多态问题,急! 一个简单而又复杂的问题??? 谢谢帮忙!!!
static class IntegerDocument extends PlainDocument { public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException { if (str == null) {
return;
}
int length = 0;
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
if (upper[i] >= '0' && upper[i] <= '9') {
upper[length++] = upper[i];
}
}
super.insertString(offs, new String(upper, 0, length), a);
}
}
throws BadLocationException { if (str == null) {
return;
}
int length = 0;
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
if (upper[i] >= '0' && upper[i] <= '9') {
upper[length++] = upper[i];
}
if (upper[i] < '0' || upper[i] > '9') {
length = 0;
break;
}
}
super.insertString(offs, new String(upper, 0, length), a);
}
}
粘贴 打印都可以限制
比如复制12ab34 就不会有任何效果
我的源代码是这样的:
private class ResultListener
implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
getAnswer();
}
public void removeUpdate(DocumentEvent e) {
getAnswer();
}
public void changedUpdate(DocumentEvent e) {
}
} //获取单价和数量文本域的内容,进行求积计算,并写入总价文本域中
public void getAnswer() {
String nu=this.number.getText().trim();
String pr=this.pric.getText().trim();
if(!nu.equals("")&&!pr.equals("")){
if(nu.matches("^\\d+$")&&pr.matches("^\\d+[\\.]?\\d{0,2}$")){
try {
int add1;
double add2,answer;
//获取第一个和第二个文本域的内容,并将其解析为数字类型变量
add1 = Integer.parseInt(nu);
add2 = Double.parseDouble(pr);
//求积
answer = add1 * add2;
DecimalFormat df2 = new DecimalFormat("###.00");
//将结果写入第三个文本域中
this.sumpric.setText(df2.format(answer));
//对于解析错误的情况,将抛出异常
}catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this,"请输入数字格式!","提示!",JOptionPane.INFORMATION_MESSAGE);
}
}else{
try {
if(!nu.matches("^\\d+$")){
JOptionPane.showMessageDialog(this,"请输入数字格式!","提示!",JOptionPane.INFORMATION_MESSAGE);
//执行下面这一语句总是抛出IllegalStateException异常
this.number.setText("0");
}
if(!pr.matches("^\\d+[\\.]?\\d{0,2}$")){
JOptionPane.showMessageDialog(this,"请输入数字格式!(最多两位小数)","提示!",JOptionPane.INFORMATION_MESSAGE);
//执行下面这一语句总是抛出IllegalStateException异常
this.pric.setText("0.00");
}
}catch (IllegalStateException e) {
System.out.println("已经捕捉到IllegalStateException异常");
}
this.sumpric.setText("0.00");
}
}
}