【发布时间】:2018-01-26 04:21:52
【问题描述】:
我有这个带有 JTextField 的 JFrame,我想让我输入的每个字母自动转换为大写 WHILE 我正在输入这个 JTextField。
我正在使用 Netbeans。
【问题讨论】:
我有这个带有 JTextField 的 JFrame,我想让我输入的每个字母自动转换为大写 WHILE 我正在输入这个 JTextField。
我正在使用 Netbeans。
【问题讨论】:
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
【讨论】: