【问题标题】:Limiting and formatting a JTextfield限制和格式化 JTextfield
【发布时间】:2011-11-20 06:23:16
【问题描述】:

我有一个JTextfield,用户在其中输入“社会安全号码”。 SSN 有 9 位数字和 2 个破折号,共 11 个字符。

如何将JTextField 限制为 11 个字符,并且在 11 个字符的第 4th 和 7th 位置的文本字段中还有 2 个破折号?

【问题讨论】:

    标签: java swing user-interface input textfield


    【解决方案1】:

    您可能想使用: JFormattedTextField

    更多帮助请参见:How to Use Formatted Text Fields

    这也可能有帮助:MaskFormatter

    这是一个示例 sn-p:

    MaskFormatter formatter = new MaskFormatter("####-###-####");
    

    编辑:

    经过一些研究,我发现 DocumentFilter 会做得更好,

    更多详情请参见:Implementing a Document Filter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多