【发布时间】:2011-01-10 09:22:24
【问题描述】:
我的 Swing 组件中有两个文本字段。在一个文本字段中,我只需要有数字 (没有字符串,空格,允许特殊字符)并且在另一个文本字段中我只需要字符串(没有数字,空格,允许特殊字符)。我该如何实现..???
【问题讨论】:
我的 Swing 组件中有两个文本字段。在一个文本字段中,我只需要有数字 (没有字符串,空格,允许特殊字符)并且在另一个文本字段中我只需要字符串(没有数字,空格,允许特殊字符)。我该如何实现..???
【问题讨论】:
【讨论】:
您有两种选择,您可以在 1) 输入时或 2) 当用户执行操作(例如单击确认按钮)时验证字段中的文本。
对于 2) npinti 的回答应该引导您朝着正确的方向前进,只需获取该字段的值并使用正则表达式对其进行验证。
对于 1),您可能想要编写一个 KeyListener 来拦截按键并只允许该字段使用正确类型的字符。
【讨论】:
您可以扩展 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的正数,并且在您按键时检查..
【讨论】: