简体   繁体   English

JButton在JTextField之前得到了关注

[英]JButton getting focused before a JTextField

在此输入图像描述

As you can see, the passwordField is positioned before jButton1 in the navigator, however, when the usernameField is focused, and I press the TAB key, the second element being focused is the jButton1 and not the passwordField . 如您所见, passwordField位于导航器中的jButton1之前,但是,当关注usernameField时,我按下TAB键,第二个被聚焦的元素是jButton1而不是passwordField

在此输入图像描述

What really controls the order of focus? 什么真正控制了焦点的顺序?

The phrase you need search for is "tab order": there is an answer to this here . 你需要搜索的短语是“Tab键顺序”:有一个答案在这里 Briefly, you will need to implement a FocusTraversalPolicy subclass, and make your container use it by calling setFocusTraversalPolicy . 简而言之,您需要实现一个FocusTraversalPolicy子类,并通过调用setFocusTraversalPolicy使您的容器使用它。

The default focus transferral is based more on the visual layout the the order of the components. 默认焦点转移更多地基于视觉布局和组件的顺序。 In this case it would want to go left to right, top to bottom. 在这种情况下,它会想要从左到右,从上到下。 This is the expected behaviour – MadProgrammer just now edit 这是预期的行为 - MadProgrammer刚刚编辑

Take a look at How to use the Focus Subsystem , especially, Customizing Focus Traversal 看看如何使用Focus子系统 ,尤其是Customizing Focus Traversal

Updated 更新

Take a look at switching JTextFields by pressing Enter key , which demonstrates the use of a custom focus policy 看一下按Enter键切换JTextFields ,它演示了自定义焦点策略的使用

尝试搜索选项卡索引或选项卡顺序以设置组件

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

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