package cn.edu.ytu.market.service;
import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/**
* 限制输入框只能输入小数
* @author Administrator
*
*/
public class DoubleDocument extends PlainDocument {
private static final long serialVersionUID = 1L;
private String reg = "^[0-9]+([.]{1}[0-9]+){0,1}$";
public void insertString(int offset, String s, AttributeSet attributeSet)
throws BadLocationException {
if (offset == 0 && s.equals(".")) {
Toolkit.getDefaultToolkit().beep();
return;
}
String str = this.getText(0, this.getLength()) + s;
int i = this.getText(0, this.getLength()).indexOf(".");
if (i == -1 && str.endsWith(".")) {
super.insertString(offset, s, attributeSet);
return;
}
if (str.matches(reg)) {
super.insertString(offset, s, attributeSet);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}
jtextField.setDcoument(new DoubleDocument())
JAVA JTextField 只能输入小数(完美)//
最新推荐文章于 2022-12-13 14:00:46 发布