是否可以将 JComponent 默认设置为不可聚焦?

发布于 2024-11-26 22:50:41 字数 365 浏览 0 评论 0原文

我知道您可以调用 JComponent.setFocusable(false) 来使 Java 组件不可聚焦。但由于我的应用程序中有很多组件我希望成为这种方式,所以我想知道是否有一种比在数十个对象中的每一个上调用它更简单的方法。就像 UIDefaults 值一样?

我正在寻找这个,因为我的应用程序全屏运行,并且 JFrame 上有一个 KeyListener 监听击键以触发各种事件。但我发现,每当单击 JButton 或其他添加的组件时,它都会获得焦点,并且关键事件永远不会到达 JFrame 。因此,一种更优雅的方式让单个父容器捕获关键事件,无论哪个子容器拥有焦点,也将有助于解决我的问题。

I know you can call JComponent.setFocusable(false) to make a Java component not be focusable. But since I have a LOT of components in my application that I want to be that way, I was wondering if there is a simpler way than calling it on every one of dozens of objects. Like a UIDefaults value?

I'm looking for this because my application runs full-screen and there is a KeyListener on the JFrame that listens for key strokes to trigger various events. But I found that whenever a JButton or other added component is clicked it would get the focus and the key events would never reach the JFrame. So a more elegant way to have key events be caught by a single parent container regardless of what child has the focus would also serve to fix my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不语却知心 2024-12-03 22:50:41

我想知道是否有一种比在数十个对象中的每一个上调用它更简单的方法。

我从来没有见过一个。

JFrame 上有一个 KeyListener,用于监听击键来触发各种事件。

不要使用 KeyListener。

相反,您可以将 JMenus 和 JMenuItems 与加速器一起使用。这种方法的好处是击键记录在菜单中。请参阅 Swing 教程中有关如何使用菜单的部分。

或者,如果您不喜欢菜单,那么您应该使用 按键绑定< /a>.即使组件没有焦点,它们也可以被编码为工作。

I was wondering if there is a simpler way than calling it on every one of dozens of objects.

I've never seen one.

there is a KeyListener on the JFrame that listens for key strokes to trigger various events.

Don't use a KeyListener.

Instead you can use JMenus and JMenuItems with accelerators. The benefit of this approach is that the key strokes are then document in the menus. See the section from the Swing tutorial on How to Use Menus.

Or if you don't like menus, then you should be using Key Bindings. They can be coded to work even if the component doesn't have focus.

你如我软肋 2024-12-03 22:50:41

听起来像是 KeyEventDispatcher 的工作:

http://download .oracle.com/javase/6/docs/api/java/awt/KeyEventDispatcher.html

这将解决问题,而不是通过(无法计算的)副作用来修改不直接相关的属性:-)

Sounds like the job for a KeyEventDispatcher:

http://download.oracle.com/javase/6/docs/api/java/awt/KeyEventDispatcher.html

That would be solving the problem, not doctoring at not directly related properties with (incalculatable) side-effects :-)

濫情▎り 2024-12-03 22:50:41

怎么样:

public boolean getComponent(Container c)
{
    Component[] cmps = c.getComponents();
    for(Component cmp : cmps)
    {
        if(cmp instanceof JComponent) // or even you could specify the JComponent you want to make it not-focusable, for example (cmp instanceof JButton)
        {
            ((JComponent)cmp).setFocusable(false);
            return true;
        }
        if(cmp instanceof Container)
        {
            if(getComponent((Container) cmp)) return true;
        }
    }
    return false;
}

然后通过以下方式调用它:

getComponent(YourJFrame);

How about:

public boolean getComponent(Container c)
{
    Component[] cmps = c.getComponents();
    for(Component cmp : cmps)
    {
        if(cmp instanceof JComponent) // or even you could specify the JComponent you want to make it not-focusable, for example (cmp instanceof JButton)
        {
            ((JComponent)cmp).setFocusable(false);
            return true;
        }
        if(cmp instanceof Container)
        {
            if(getComponent((Container) cmp)) return true;
        }
    }
    return false;
}

Then just call it by:

getComponent(YourJFrame);
一萌ing 2024-12-03 22:50:41

这是一种相当粗略的遍历树并将其设置在您关心的元素上的方法。只需传入一组 noFocus (或将逻辑更改为一切)...

  public static List<Component> disableFocus(final Container c, Set<Component> noFocus) {
    Component[] comps = c.getComponents();
    for (Component comp : comps) {
      if (noFucus.contains(comp) { comp.setFocusable(false); }
      if (comp instanceof Container) {
        disableFocus(comp);
      }
    }
  }

Here is a rather crude way of walking the tree and setting it on elements you care about. Just pass in a set of noFocus (or change the logic to just be everything)...

  public static List<Component> disableFocus(final Container c, Set<Component> noFocus) {
    Component[] comps = c.getComponents();
    for (Component comp : comps) {
      if (noFucus.contains(comp) { comp.setFocusable(false); }
      if (comp instanceof Container) {
        disableFocus(comp);
      }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文