如果一个文本框里要求只能输入数字,要怎么做呀?类似于QQ的登录界面的那个输入QQ号那里一样,只要输入非数字就不能输入.这个在java里要怎么实现呀?
解决方案 »
- 求助了~这个程序运行的时候说 main里的数组越界~请高手帮忙看看,还有必须用-Xlint才能编译,为啥呢?小弟感激不尽
- 如何用swing实现jtable的列合并
- nextInt*()函数源代码的解释 求助
- 请问用j2se能实现 输出 图表么? 饼状或柱状图.
- 一个关于java Swing的问题,在线等!
- dom4j修改删除xml的问题,在线等
- jxl.jar读取图表
- Non-Blocking SocketChannel的问题
- 如何把用drawString画的字符串在网页上做成链接?谢谢!
- 关于爬虫模拟登陆
- 如何快速将数据导入excel问题, 在线拜求解答
- JAVA如何访问MYSQL的存储过程
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
public class TestInputNumber {
public static void main(String[] args) {
FormatTestFrame frame = new FormatTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class FormatTestFrame extends JFrame {
public FormatTestFrame() {
setTitle("FormatTest");
setSize(300, 100);
Container contentPane = getContentPane();
JLabel lblIntField = new JLabel("NumberFilter:");
JFormattedTextField intField
= new JFormattedTextField(new
InternationalFormatter(
NumberFormat.getIntegerInstance()) {
protected DocumentFilter getDocumentFilter() {
return filter;
}
private DocumentFilter filter = new IntFilter();
});
intField.setValue(new Integer(100));
intField.setColumns(10);
JPanel mainPanel = new JPanel();
mainPanel.add(lblIntField);
mainPanel.add(intField);
contentPane.add(mainPanel, BorderLayout.CENTER);
}
class IntFilter extends DocumentFilter {
public void insertString(FilterBypass fb, int offset,
String string, AttributeSet attr)
throws BadLocationException {
StringBuffer buffer = new StringBuffer(string);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER)
buffer.deleteCharAt(i);
}
super.insertString(fb, offset, buffer.toString(), attr);
}
public void replace(FilterBypass fb, int offset,
int length, String string, AttributeSet attr)
throws BadLocationException {
if (string != null) {
StringBuffer buffer = new StringBuffer(string);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER)
buffer.deleteCharAt(i);
}
string = buffer.toString();
}
super.replace(fb, offset, length, string, attr);
}
}
}
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);
}
}