当前位置: 技术问答>java相关
jTextField 的输入问题?swing高手请进.
来源: 互联网 发布时间:2015-09-02
本文导语: 要求输入位数控制在6位,再输入时jTextField中不能显示.getText()也不能读出. | 先写一个document类: import javax.swing.text.*; import java.awt.*; class LimitedLengthDocument extends PlainDocument { private length...
要求输入位数控制在6位,再输入时jTextField中不能显示.getText()也不能读出.
|
先写一个document类:
import javax.swing.text.*;
import java.awt.*;
class LimitedLengthDocument extends PlainDocument
{
private length ;
public LimitedLengthDocument(int len)
{
this.length = len;
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if (str == null) {
return;
}
// Get current value
String curVal = getText(0, getLength());
if ( curVal.length() > length )
return;
else
super.insertString(index, s, a);
}
用法:JTextField txtbox = new JTextField();
txtbox.setDocument(new LimitedLengthDocument(6));
import javax.swing.text.*;
import java.awt.*;
class LimitedLengthDocument extends PlainDocument
{
private length ;
public LimitedLengthDocument(int len)
{
this.length = len;
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if (str == null) {
return;
}
// Get current value
String curVal = getText(0, getLength());
if ( curVal.length() > length )
return;
else
super.insertString(index, s, a);
}
用法:JTextField txtbox = new JTextField();
txtbox.setDocument(new LimitedLengthDocument(6));
|
http://www.csdn.net/expert/topic/973/973510.xml?temp=.7644464