键入时将文本转换为大写

问题描述 投票:-4回答:1

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

我正在使用Netbeans。

java netbeans textfield
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.