如何让JTextField里只能输入字符串

feifeiget 2007-08-22 10:47:33
如何让JTextField里只能输入字符串,主要是不能输入数字。即如何判断输入的是数字还是字符串。
...全文
245 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
emin_lee 2007-08-28
  • 打赏
  • 举报
回复
mark!
esprit0318 2007-08-28
  • 打赏
  • 举报
回复
mark
huanzhugege 2007-08-27
  • 打赏
  • 举报
回复
可以看看《J2EE开发全程实录》这本书,它的源码可以在网站下载,里边对文本框组件的使用做了详细的介绍。
http://book.csdn.net/bookfiles/427/index.html
zwgs1985 2007-08-27
  • 打赏
  • 举报
回复
重写这个方法public void insertString(int offset, String str, AttributeSet attributeSet)
在里面判断str是否符合要求,不符合直接return,粘贴不进去的
wzlsquall 2007-08-23
  • 打赏
  • 举报
回复
package jp.co.hitachi_system.ias.pulsewave.cv;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.Toolkit;
import java.util.regex.Pattern;

/**
* Java Text Document:use regular expression to validate input value
*/
public class PulseWaveTextDocument extends PlainDocument {

private static final long serialVersionUID = -4462693078138709956L;

/**
* パタン ログインIDとパスワード
*/
public static final String PATTERN_LOGIN = "[^,\\s]*";

/**
* パタン 数字
*/
public static final String PATTERN_DECIMAL = "(\\d+)(\\.)?(\\d*)";

/**
* パタン 年
*/
public static final String PATTERN_YEAR = "\\d*";

/**
* パタン 月
*/
public static final String PATTERN_MONTH = "\\d*";

/**
* パタン 日
*/
public static final String PATTERN_DAY = "\\d*";

/**
* input regular expression
*/
private String pattern = null;

/**
* maxLength, -1 no limit
*/
private int maxLength = -1;

/**
* max number value limit
*/
private double maxValue = 0;

/**
* whether limit max value
*/
private boolean isMaxValue = false;

/**
* Toolkit beep
*/
private Toolkit toolkit = null;

/**
* 改行入力の抑止チェックフラグ
*/
private boolean enterCheck = false;

/**
* whether beep
*/
private boolean beep = false;

public void setEnterCheck(boolean enterCheck){
this.enterCheck = enterCheck;
}

public PulseWaveTextDocument() {
super();
this.init();
}

public PulseWaveTextDocument(Content c) {
super(c);
this.init();
}

/**
* 初期化
*/
private void init() {
toolkit = Toolkit.getDefaultToolkit();
}

public void setCharPattern(String pattern) {
this.pattern = pattern;
}

public String getCharPattern() {
return this.pattern;
}

public void clearPattern() {
this.pattern = null;
}

public boolean isOfPattern(CharSequence input) {
if (pattern == null) {
return true;
} else {
return Pattern.compile(pattern, Pattern.CANON_EQ).matcher(input).matches();
}
}

public boolean isEmptyLimit() {
if (pattern == null) {
return true;
} else {
return false;
}
}

public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}

public void cancelMaxLength() {
this.maxLength = -1;
}

public void setMaxValue(double maxValue) {
this.isMaxValue = true;
this.maxValue = maxValue;
}

public boolean isMaxValue() {
return this.isMaxValue;
}

public double getMaxValue() {
return this.maxValue;
}

public void cancelMaxValue() {
this.isMaxValue = false;
this.maxValue = 0;
}

public void reset() {
clearPattern();
cancelMaxLength();
cancelMaxValue();
}

public void errorBeep(boolean beep) {
this.beep = beep;
}

public boolean isErrorBeep() {
return beep;
}

public void insertString(int offSet, String sourceString, AttributeSet attributeSet) throws BadLocationException, NumberFormatException {
// 文字列がNULL場合
if (sourceString == null) {
return;
}

boolean b = true;

String s = this.getText(0, this.getLength());
s = s.substring(0, offSet) + sourceString + s.substring(offSet, s.length());

// 文字列が無効場合
if (!isOfPattern(s)) {
b = false;
} else if (maxLength > -1 && this.getLength() + sourceString.length() > maxLength) {
b = false;
} else if (isMaxValue) {
// 最大値制限有無
if (Double.parseDouble(s) > maxValue) {
b = false;
}
}else if(enterCheck && sourceString.indexOf("\n") >= 0){
b = false;
}

// 文字列が無効場合
if (!b) {
if (beep) {
// beep
toolkit.beep();
}
} else {
super.insertString(offSet, sourceString, attributeSet);
}
}
}
Ji秋风 2007-08-23
  • 打赏
  • 举报
回复
看看这个
http://blog.csdn.net/irvine007/archive/2005/05/13/374235.aspx
mq612 2007-08-22
  • 打赏
  • 举报
回复
我 blog 里有个例子
http://blog.csdn.net/mq612/archive/2006/09/29/1305413.aspx
zdjray 2007-08-22
  • 打赏
  • 举报
回复
输入的时候检查每个字符是不是数字
zdjray 2007-08-22
  • 打赏
  • 举报
回复
输入的时候检查每个字符是不是数组
insiku 2007-08-22
  • 打赏
  • 举报
回复
重载insertString
但是
ctrl v 还是可以复制进去

62,623

社区成员

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

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