简体   繁体   English

Java-使文本框不失去焦点

[英]Java - making a textbox not lose focus

I have a few different input textboxes which you can enter a command into, press enter, and then it pulls up some information. 我有几个不同的输入文本框,您可以在其中输入命令,按Enter,然后弹出一些信息。 I have a few different textboxes who (applying to different parts of information). 我有几个不同的文本框(适用于信息的不同部分)。 When I press enter, it also moves the focus onto the next input box. 当我按Enter键时,它还将焦点移到下一个输入框。 How do I stop this? 如何停止呢? I can't simply use the consume() method in the keyevent class since that would entirely block the enter key, which is not what I want 我不能简单地在keyevent类中使用consumer()方法,因为那样会完全阻塞Enter键,这不是我想要的

InputVerifier , described in Validating Input , is designed for exactly this purpose: 验证输入中描述的InputVerifier正是用于此目的:

A component's input verifier is consulted whenever the component is about to lose the focus. 每当组件即将失去焦点时,都会咨询组件的输入验证程序。 If the component's value is not acceptable, the input verifier can take appropriate action, such as refusing to yield the focus on the component… 如果组件的值不可接受,则输入验证程序可以采取适当的措施,例如拒绝将注意力集中在组件上……

Addendum: As suggested in the article Text Validation , InputVerifier should be an integral part of validation, designed so that the user knows immediately why something is awry. 附录:如“ 文本验证 ”一文中建议的那样, InputVerifier应该是验证的组成部分,其设计目的是使用户立即知道出现问题的原因。

Actually, I just found out how to fix this issue. 实际上,我只是找到了解决此问题的方法。 I used the method belonging to Container called requestFocus() (see http://www.javaworld.com/javaworld/jw-07-1998/jw-07-swing-focus.html ). 我使用了属于Container的称为requestFocus()的方法(请参见http://www.javaworld.com/javaworld/jw-07-1998/jw-07-swing-focus.html )。 In each element I added this line to the end of the keyEvent action and it worked perfectly! 在每个元素中,我将这一行添加到keyEvent操作的末尾,并且效果很好!

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

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