【问题标题】:Convert text to Uppercase while typing键入时将文本转换为大写
【发布时间】:2018-01-26 04:21:52
【问题描述】:

我有这个带有 JTextField 的 JFrame,我想让我输入的每个字母自动转换为大写 WHILE 我正在输入这个 JTextField。

我正在使用 Netbeans。

【问题讨论】:

    标签: java netbeans textfield


    【解决方案1】:
    class UpperCaseDocument extends PlainDocument {
      private boolean upperCase = true;
    
      public void setUpperCase(boolean flag) {
        upperCase = flag;
      }
    
      public void insertString(int offset, String str, AttributeSet attSet)
          throws BadLocationException {
        if (upperCase)
          str = str.toUpperCase();
        super.insertString(offset, str, attSet);
      }
    
    }
    
    
    JTextField tf = new JTextField(20);
    UpperCaseDocument ucd = new UpperCaseDocument();
    //Associates the editor with a text document. 
    tf.setDocument(ucd);
    

    来源:Force JTextField to convert input to upper case with PlainDocument in Java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 2013-12-02
      • 2021-02-02
      相关资源
      最近更新 更多