简体   繁体   English

如何防止Java Swing中的工具栏按钮焦点?

[英]How to prevent toolbar button focus in Java Swing?

In my Java Swing application I am using a JToolBar with several buttons. 在我的Java Swing应用程序中,我使用带有几个按钮的JToolBar After the toolbar object is being created I call setFocusable(false) . 创建工具栏对象后,我调用setFocusable(false) However, every time I start the application the first button in the toolbar is focused. 但是,每次我启动应用程序时,工具栏上的第一个按钮都会被聚焦。

Any ideas how I can prevent the toolbar buttons to come into focus at all? 有什么想法可以防止工具栏按钮完全成为焦点吗?

Firstly, Toolbar buttons should not have 首先,工具栏按钮不应包含

setFocusable(false)

called on them, as it breaks keyboard support. 调用它们,因为它破坏了键盘的支持。 Rather, call: 而是,致电:

setRequestFocusEnabled(false)

which will stop them from taking over the focus when the mouse clicks them, but still lets keyboard-centric users "tab over" to them. 当鼠标单击它们时,这将阻止它们接管焦点,但仍然允许以键盘为中心的用户“跳转”到它们。

You probably want to select some other sensible component to place the initial keyboard focus onto. 您可能想要选择其他一些明智的组件来将初始键盘焦点放在该组件上。 So just call: 因此,只需致电:

otherComponent.requestFocusInWindow();

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

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