限制 JTextField 中的字符数 [英] Limit number of characters in JTextField

查看:33
本文介绍了限制 JTextField 中的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过使用将允许的字符数限制为 5 个

I try to limit the number of allowable characters to 5 by using

    try {
        jFormattedTextField2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("*****")));
    } catch (java.text.ParseException ex) {
        ex.printStackTrace();
    }

然而,当我只输入 1 个字符时,总会有 4 个其他空格被填充.如何避免填充,同时限制字符数?

However, when I only enter 1 character, there will always be 4 others space being padded. How can I avoid the padding, at the same time limiting number of characters?

推荐答案

JFormattedTextField 导致糟糕的 UI.

你想要的类是javax.swing.text.DocumentFilter.这允许您以命令链样式修改突变.

The class you want is javax.swing.text.DocumentFilter. This allows you modify mutations in a chain-of-command style.

这篇关于限制 JTextField 中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆