我的swing组件中有两个textfield。在一个文本字段中,我只需要数字 (没有字符串,空格,允许特殊字符)而在另一个文本字段中我只需要字符串(没有数字,空格,允许特殊字符)。我该如何实现.. ???
答案 0 :(得分:1)
您可以使用Pattern类(正则表达式)来验证输入。我们提供了一个简短的教程here。
我很确定基础教程涵盖了所有这些......
"^//d+$" //The text must have at least one digit, no other characters are allowed
"^[a-zA-Z]+$" //The text must have at least one letter, no other characters are allowed
答案 1 :(得分:1)
您有两种选择,您可以在输入时验证字段中的文本,或者在用户执行操作(例如单击确认按钮)时验证字段中的文本。
对于2)npinti的答案应该引导你朝着正确的方向,只需获取字段的值并用正则表达式验证它。
对于1)你可能想写一个截取按键的KeyListener
,并且只允许该字段的正确字符类型。
答案 2 :(得分:0)
您可以扩展javax.swing.text.PlainDocument类,并调用setDocument方法textfield。这是一个例子;
package textfield;
import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class LimitedValuePositiveIntegerDocument extends PlainDocument {
int maxValue;
int maxLength;
Toolkit toolkit;
/**
* Constructor for the class.
* @param max maximum value of the number
*/
public LimitedValuePositiveIntegerDocument(int max){
maxValue = max;
maxLength = (""+max).length();
toolkit = Toolkit.getDefaultToolkit();
}
/**
* Inserts the input string to the current string after validation.
* @param offs offset of the place where the input string is supposed to be inserted.
* @param str input string to be inserted
* @param a attribute set
*/
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if(str == null)return;
String currentText = getText(0,getLength());
String resultText = new String();
int i;
boolean errorFound = false;
boolean deleteFirstZero = false;
int accepted=0;
for (i = 0; (i<str.length())&&(!errorFound); i++) {
if (Character.isDigit(str.charAt(i))) { /* if it is digit */
if (offs==currentText.length()) { /* calculates the resultant string*/
resultText = currentText+str.substring(0,i+1);
} else if (offs==0) {
resultText = str.substring(0,i+1)+currentText;
} else {
resultText = currentText.substring(0, offs)+str.substring(0,i+1)+currentText.substring(offs,currentText.length());
}
if (Integer.parseInt(resultText) > maxValue) {
errorFound = true;
toolkit.beep();
} else {
if ( resultText.length() == maxLength+1) {
deleteFirstZero = true;
}
accepted++;
}
} else {
errorFound = true;
toolkit.beep();
}
}
if ( accepted>0 ) { /* insert string */
super.insertString(offs, str.substring(0,accepted), a);
if (deleteFirstZero) {
super.remove(0,1);
}
}
}
/**
* Removes a part of the current string.
* @param offs offset of the place to be removed.
* @param len length to be removed
*/
@Override
public void remove(int offs, int len) throws BadLocationException{
super.remove(offs, len);
}
/**
* Returns max value of the number.
* @return max value
*/
public int getMaxValue() {
return maxValue;
}
/**
* Sets max value of the number.
* @param max maximum value of the number
*/
public void setMaxValue(int max) {
this.maxValue = max;
}
} // end of class
编辑: 及其用法;
LimitedValuePositiveIntegerDocument doc = new LimitedValuePositiveIntegerDocument(999);
JTextField numberField = new JtextField();
numberField.setDocument(doc);
您只能输入小于1000的正数,并在按键时进行检查..