识别特定屏幕坐标处的Swing组件? (并手动调度MouseEvents) [英] Identifying Swing component at a particular screen coordinate? (And manually dispatching MouseEvents)

查看:184
本文介绍了识别特定屏幕坐标处的Swing组件? (并手动调度MouseEvents)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些工作,使Java应用程序与其他输入设备兼容。不幸的是,这个设备有一个Java API,它现在几乎没有进入alpha阶段,所以它非常糟糕。我需要做的是基本上为MouseEvents的发送设置一个替换结构。有没有人知道Swing是否有办法获取屏幕坐标并找出在该屏幕点顶部显示的Swing组件?

I'm doing some work making a Java app compatible with alternative input devices. Unfortunately, the device in question has a Java API that's barely into the alpha stages right now, so it's pretty poor. What I need to do is essentially set up a replacement structure for the dispatch of MouseEvents. Does anyone know if there's a way in Swing to take a screen coordinate and find out what Swing component is displayed on top at that screen point?

推荐答案

在AWT容器中,调用此...

In AWT Container, call this ...

findComponentAt(int x, int y) 
          Locates the visible child component that contains the specified position

即如果它在GlassPane中......

i.e. If it is in a GlassPane...

  public static Component findComponentUnderGlassPaneAt(Point p, Component top) {
    Component c = null;

    if (top.isShowing()) {
      if (top instanceof RootPaneContainer)
        c =
        ((RootPaneContainer) top).getLayeredPane().findComponentAt(
            SwingUtilities.convertPoint(top, p, ((RootPaneContainer) top).getLayeredPane()));
      else
        c = ((Container) top).findComponentAt(p);
    }

    return c;
  }

阅读你的问题,这对你也有帮助...

Reading your question, this might be of help to you also...

如果你想锻炼控制使用这个...
Java.awt.Robot类用于控制鼠标和键盘。获得控件后,您可以通过Java代码执行与鼠标和键盘相关的任何类型的操作。该类通常用于测试自动化。

If you want to exercise the control use this ... Java.awt.Robot class is used to take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through your java code. This class is used generally for test automation.

这篇关于识别特定屏幕坐标处的Swing组件? (并手动调度MouseEvents)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆