简体   繁体   English

如何停止TextField接受输入-AWT

[英]How to stop TextField from accepting input - AWT

I have a textField in which I input a username, and I want the screen to change when the 'enter' key is pressed. 我有一个textField,我在其中输入了用户名,并且我希望按“ enter”键时屏幕可以更改。 But what I noticed is that the KeyPressed method doesn't start, because the enter key is taken as input into the textfield. 但是我注意到的是KeyPressed方法没有启动,因为回车键被当作输入到文本字段的方法。

Is there a way for me to loose focus on a TextField when the enter key is pressed, even if the KeyPressed method doesn't work while focused on the textfield? 当按下Enter键时,即使KeyPressed方法在聚焦于文本字段时不起作用,我是否有办法放松对TextField的聚焦?

You may use an ordinary event handler like this: 您可以使用像这样的普通事件处理程序:

textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });

A TextField action is different from a button action. TextField操作不同于按钮操作。 A TextField action checks the Enter key for any actions, unlike a button, which checks for input through the mouse. TextField操作会检查Enter键是否有任何操作,与按钮不同,按钮是通过鼠标检查输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM