在 Java 中将文本字段中的所有字母大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2874821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:14:35  来源:igfitidea点击:

Capitalize all letters in a Textfield in Java

javaswingtext

提问by Gordon

Is it to possible to capitalize the letters in a Textfield as they are being typed by the user in Java?

当用户在 Java 中键入文本字段中的字母时,是否可以将它们大写?

E.g. The user would type 'hello' and 'HELLO' would appear in the Textfield.

例如,用户将键入“hello”,“HELLO”将出现在文本字段中。

(Odd request and I don't like the idea either).

(奇怪的要求,我也不喜欢这个主意)。

回答by Glennular

回答by user2266204

Try

尝试

jTextField.addKeyListener(new KeyAdapter() {

  public void keyTyped(KeyEvent e) {
    char keyChar = e.getKeyChar();
    if (Character.isLowerCase(keyChar)) {
      e.setKeyChar(Character.toUpperCase(keyChar));
    }
  }

});

回答by Daniel

ModifyListenerand getText().toUpperCase()are your friends.

ModifyListenergetText().toUpperCase()是你的朋友。

回答by corsiKa

This is probably an inefficient way to do it

这可能是一种低效的方法

but you could have a section in your KeyTyped event handler

但是您可以在 KeyTyped 事件处理程序中有一个部分

if(event.getSource() == capitalTextArea) {
    String text = capitalTextArea.getText();
    if(Character.isLowerCase(text.charAt(text.length()-1))) {
        capitalTextArea.setText(text.toUpperCase());
    }
}

I might have syntatical mistakes, but that's the apporach i'd take

我可能有语法错误,但这就是我要采取的方法

回答by SOLIHIN

Try

尝试

private void inText_UserIDKeyReleased( java.awt.event.KeyEvent evt ) {
    String UsrID=inText_UserID.getText().toUpperCase();
    inText_UserID.setText( UsrID );
}

回答by Naiguel Santos

A help for friends who find it interesting: how to make letters written in TextField capitalized. Ex: Legend:

对感兴趣的朋友的帮助:如何使TextField中的字母大写。例如:图例:

txtCadastrarNome= name of the variable of text field.

txtCadastrarNome= 文本字段变量的名称。

txtCadastrarNomeKeyTyped= action when it is being typed from the keyboard.

txtCadastrarNomeKeyTyped= 从键盘输入时的动作。

private void txtCadastrarNomeKeyTyped(java.awt.event.KeyEvent evt) { 
txtCadastrarNome.setText(txtCadastrarNomeCliente.getText().toUpperCase());
}