桌面程序中,如何限定一个输入框只能输入10个字符,超过10个字符时,再按键或粘贴都不显示?

dreamsky 2006-03-31 09:46:15
如题
...全文
389 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gow 2006-04-07
  • 打赏
  • 举报
回复
同意
voxer(voxer)和cuij7718(沸腾的音乐 http://sunfruit.blogchina.com) !!
dreamsky 2006-04-06
  • 打赏
  • 举报
回复
使用Document对象如何设置呢?
atext.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent event) {
if (atext.getText().length() > alen) {
String ainfo = atext.getText();
atext.setText(ainfo.substring(0, alen));
}
}})
提示错误,不允许atext.setText
cuij7718 2006-04-06
  • 打赏
  • 举报
回复
voxer(voxer) 的方法是正解,用Key监听器不是很好
捏造的信仰 2006-04-06
  • 打赏
  • 举报
回复
寒死,什么样的方法都来了……

正解(少数变量定义省略了):

tfield.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
JTextField source = (JTextField) e.getSource();

int length = (source).getText().length();
if (length >= 10) {
// 取消键入
e.consume();

if (length > 10) {
//截取粘贴
source.setText(source.getText().substring(0, 10));
}
}
}

public void keyPressed(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
}

});
Voxer 2006-04-06
  • 打赏
  • 举报
回复
import java.awt.Toolkit;
import javax.swing.text.*;
import javax.swing.*;
//
public class TenLimitDocument
extends PlainDocument {
JTextField txtFld;
public TenLimitDocument(JTextField txtFld) {
this.txtFld = txtFld;
}

public void insertString(int offset, String s, AttributeSet attributeSet) throws
BadLocationException {
if(txtFld.getText().length()>=10){
return ;
}
super.insertString(offset,s,attributeSet);
}
}
用的时候
JTextFiled txtFld = new JTextField();
txtFld.setDocument(new TenLimitedDoucment(txtFld));
yushaofeng00 2006-04-04
  • 打赏
  • 举报
回复
自己写个算法吧~例如
import java.awt.*;
import java.awt.event.*;
public class MyApp
{
public static void main(String args[])
{
mFrame mf=new mFrame();
}
}
class mFrame extends Frame implements TextListener
{
TextField t=new TextField();
TextField t1=new TextField(6);
mFrame()
{
setLayout(new FlowLayout());
t.addTextListener(this);
t.setColumns(6);
add(t);
add(t1);
pack();
show();
}
public void textValueChanged(TextEvent e)
{
if(e.getSource()==t)
{
String s=new String(t.getText());
int length=s.length();
if(length>5)
{
s=s.substring(0,length-1);
t.setText(s);
length=5;
}
}
}
public boolean handleEvent(Event e)
{
if(e.id==Event.WINDOW_DESTROY)
{
dispose();
System.exit(0);
}
return super.handleEvent(e);
}
}
syhan 2006-04-03
  • 打赏
  • 举报
回复
你初始化的时候限定只能10个不就是了
JTextField txt = new JTextFiled(10);
这样不行吗?还是搂主的意思是可以输入超过10个,但是超过的部分不显示呢?
cuij7718 2006-04-03
  • 打赏
  • 举报
回复
使用Document对象限制
Nuage 2006-03-31
  • 打赏
  • 举报
回复
public JFormattedTextField locFormat(JFormattedTextField locationIn){
JFormattedTextField locationOut=locationIn;
try {
MaskFormatter locaFormat = new MaskFormatter("##########");
locationOut = new JFormattedTextField(locaFormat);
} catch (ParseException e) {
e.printStackTrace();
}

return locationOut;
}
监听里面加上这个.不过这样一来必须输入10个字符.楼主看看有没有用.
  • 打赏
  • 举报
回复
监听Text输入的长度,当等于10的时候,让不可用应该可以
diyucity 2006-03-31
  • 打赏
  • 举报
回复
完了吗?不是这样的吧?
dreamsky 2006-03-31
  • 打赏
  • 举报
回复
UP

62,626

社区成员

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

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