我正在做一些工作,使 Java 应用程序与其他输入设备兼容。不幸的是,有问题的设备有一个 Java API,现在几乎没有进入 alpha 阶段,所以它很差。我需要做的基本上是为 MouseEvents 的调度设置一个替换结构。有谁知道 Swing 中是否有一种方法可以获取屏幕坐标并找出在该屏幕点顶部显示的 Swing 组件?
最佳答案
在 AWT 容器中,调用这个 ...
findComponentAt(int x, int y)
Locates the visible child component that contains the specified position
即如果它在 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;
}
阅读您的问题,这可能对您也有帮助...
如果你想行使控制权,使用这个... Java.awt.Robot 类用于控制鼠标和键盘。一旦你获得了控制权,你就可以通过你的java代码进行任何类型的与鼠标和键盘相关的操作。此类通常用于测试自动化。
关于java - 识别特定屏幕坐标处的 Swing 组件? (并手动调度 MouseEvents),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2733896/