如何控制一个JTextField中输入的字符(或数字)不超过15位?我是这样做的,高手过来瞧一眼!

xiaoyongjun 2004-08-10 02:37:43
如何在这里面控制一个JTextField中输入的字符(或数字)不超过15位?

public class JTelDocument extends PlainDocument
{
//重载insertString方法,限制它只能输入0-9数字、"("、")"、"-"符号
public void insertString(int index, String str, AttributeSet as) throws BadLocationException
{
if ((str == null) || (str.length() == 0))
return;

char[] input = str.toCharArray(); //转化成字符数组
int length = 0; //合法输入的数组长度
for (int i = 0; i < input.length; i++)
{
char ch = input[i];
if (ch >= '0' && ch <= '9')
input[length++] = ch;
if (ch == '(' || ch == ')' || ch == '-')
input[length++] = ch;
}
super.insertString(index, new String(input, 0, length), as);
}
}

我想在这里面控制,怎么控制呢?高手给点意见吧。
...全文
339 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hondajiang 2005-01-12
  • 打赏
  • 举报
回复
如果只是要求String的长度达到15的话,可以直接改写一个PlainDocument类:
jTextFiled.setDocument(new PlainDocument(){
public void insertString (int offset, String str, AttributeSet a) {
try {
if (this.getLength() + str.length() > 15){
return;
}
else{
super.insertString(offset, str, a);
}
}
catch (Exception exp) {
}
}
});
就可以了.我实验过,对于字母,数字还有符号都可以.
不过,之后出现了一个大BUG,我试验了很多方法,怀疑是SWING的问题,就是输入全角字符会导致之前输入的文字被删除.好象是自动的.不管是输入中文或者日文都一样.郁闷中
5257007 2004-09-13
  • 打赏
  • 举报
回复
弱智!!

这都不知道!!

睡着想想啊!!!

笨!!提示在你信息里!!笨!!
registered 2004-08-10
  • 打赏
  • 举报
回复
private class SizeFilter extends DocumentFilter {
int maxSize;
int size;
Pattern p = Pattern.compile("[0-9()-]");
Matcher m = p.matcher("");

public SizeFilter(int maxSize) {
this.maxSize = maxSize;
}

public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
size = fb.getDocument().getLength() + string.length();
m.reset(string);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, string, attr);
}
}

public void replace(DocumentFilter.FilterBypass fb, int offset,
int length,String text, AttributeSet attrs)
throws BadLocationException {
size = fb.getDocument().getLength() + text.length();
m.reset(text);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, text, attrs);
}
}
}

用这个替换上面那个 SizeFilter 可以完全达到你的要求
registered 2004-08-10
  • 打赏
  • 举报
回复

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;


public class DocSizeFilter extends JFrame {
public static final int MAX_SIZE = 10;
private JTextField jtf;

public DocSizeFilter() {
AbstractDocument aDoc;

jtf = new JTextField();
aDoc = (AbstractDocument) jtf.getDocument();
aDoc.setDocumentFilter(new SizeFilter(MAX_SIZE));
getContentPane().add(jtf);
}

public static void main(String[] args) {
DocSizeFilter frame = new DocSizeFilter();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

private class SizeFilter extends DocumentFilter {
int maxSize;
int size;

public SizeFilter(int maxSize) {
this.maxSize = maxSize;
}

public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
size = fb.getDocument().getLength() + string.length();
if (size < maxSize) {
super.insertString(fb, offset, string, attr);
}
}

public void replace(DocumentFilter.FilterBypass fb, int offset,
int length,String text, AttributeSet attrs)
throws BadLocationException {
size = fb.getDocument().getLength() + text.length();
if (size < maxSize) {
super.insertString(fb, offset, text, attrs);
}
}
}
}
xiaoyongjun 2004-08-10
  • 打赏
  • 举报
回复
别的高手有什么意见?

上来说说呀,

我很着急呀!
xiaoyongjun 2004-08-10
  • 打赏
  • 举报
回复
String textValue = OnlyInputNumberField.this.getText();

这是什么意思?

从你的代码中看不出如何控制所输入的数字不超过某个数?请指点!谢谢了!
sealwzq 2004-08-10
  • 打赏
  • 举报
回复
这是我自己写的一个类,希望对你有点参考价值:

package myjava;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
* <p>Title: Control of the text field!
* <p>Description: In this text field,
* if the key that you push is not a number,
* it will can't be disposed in the field!
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 科大恒星
* @author 呉志強
* @version 1.0
*/

public class OnlyInputNumberField extends JTextField
{
int givenLenght;
/** If the key have been pushed it will have do something.
* @param e The event of the key pushed.
*/
public OnlyInputNumberField() {
this.addKeyListener(new KeyAdapter() {

/**
* This method it will judge
* whether or not the key you pushed is one of the keys of number
* @param e The key event that you input
*/
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();//Get name of the key that have been pushed.
String textValue = OnlyInputNumberField.this.getText();
int len = textValue.length();
if (c =='\b')
{
textValue=textValue.substring(0,len);
OnlyInputNumberField.this.setText(textValue);
}
else
{
if (c >= '0' && c <= '9')
return;
else
e.consume();
}
}
});
}

public OnlyInputNumberField(int i)
{
givenLenght = i;

this.addKeyListener(new KeyAdapter()
{
/**
* This method it will judge
* whether or not the lenght is over the given lenght
* @param e The key event that you push.
*/
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();//Get name of the key that have been pushed.
String textValue = OnlyInputNumberField.this.getText();
int len = textValue.length();
if (c =='\b')
{
textValue=textValue.substring(0,len);
OnlyInputNumberField.this.setText(textValue);
}
else
{
if (c >= '0' && c <= '9')
{
if (len < givenLenght) {
//System.out.println("the number");
return;
}
else
e.consume();
}
else
e.consume();
}
}
});
}

}
sealwzq 2004-08-10
  • 打赏
  • 举报
回复
用KeyListener就可以了~~~然后用一个整形来控制输入的长度,每输一个就判断一次
xiaoyongjun 2004-08-10
  • 打赏
  • 举报
回复
兄弟姐妹们,都来看一看呀,帮我解决解决这个问题呀,看起来不是很难呀?
xiaoyongjun 2004-08-10
  • 打赏
  • 举报
回复
现在的问题是在我的这段代码里怎么判断我目前已经输入了15个字符,
我采用了这种方法,但是总有问题,就是当我录入超过15个字符后,我把刚才录入的内容清空(或删除几个),这时还是不能录入字符,因为我定义的变量是类变量(Static变量),我刚才说的情况怎么判断呢?我的代码如下:

public class JTelDocument extends PlainDocument
{
public static int intCount = 0;
//重载insertString方法,限制它只能输入0-9数字、"("、")"、"-"符号
public void insertString(int index, String str, AttributeSet as) throws BadLocationException
{
if ((str == null) || (str.length() == 0))
{
intCount = 0;
return;
}
if (intCount > 15)
return;

char[] input = str.toCharArray(); //转化成字符数组
int length = 0; //合法输入的数组长度
for (int i = 0; i < input.length; i++)
{
char ch = input[i];
if (ch >= '0' && ch <= '9')
{
input[length++] = ch;
intCount++;
}
if (ch == '(' || ch == ')' || ch == '-')
{
input[length++] = ch;
intCount++;
}
}
super.insertString(index, new String(input, 0, length), as);
}
}
张海霖 2004-08-10
  • 打赏
  • 举报
回复
超过就删除最后一个字符

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧