我正在做一些工作,使Java應用程序與其他輸入設備兼容。不幸的是,有問題的設備有一個Java API,它現在幾乎不在alpha階段,所以它很差。我需要做的是基本上爲MouseEvent的派發建立一個替換結構。有誰知道在Swing中是否有方法獲取屏幕座標,並找出在該屏幕點上顯示的Swing組件?識別特定屏幕座標處的Swing組件? (並手動調度MouseEvents)
10
A
回答
15
在AWT容器,稱此...
findComponentAt(int x, int y)
Locates the visible child component that contains the specified position
即如果它是在玻璃面板...
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代碼進行與鼠標和鍵盤相關的任何類型的操作。這個類通常用於測試自動化。
3
另一種(可能需要進一步的調整):
public static Component findComponentUnderMouse() {
Window window = findWindow();
Point location = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(location, window);
return SwingUtilities.getDeepestComponentAt(window, location.x, location.y);
}
private static Window findWindow() {
for (Window window : Window.getWindows()) {
if (window.getMousePosition(true) != null)
return window;
}
return null;
}
相關問題
- 1. 從屏幕上的特定座標處獲取文本
- 2. 從主屏幕識別手勢
- 3. 在屏幕/特定座標之間移動WPF應用程序
- 4. 捏縮放和平移,以特定的屏幕座標
- 5. Mapkit經緯度到屏幕座標
- 6. 目標特定的啓動屏幕
- 7. 屏幕座標和設備座標有什麼區別?
- 8. 將屏幕座標轉換爲座標
- 9. 排序屏幕座標
- 10. 更改屏幕座標
- 11. OpenGL座標匹配屏幕座標?
- 12. Android:在屏幕鎖定時識別手勢
- 13. C#如何獲取屏幕上特定點的座標。 (不是鼠標位置)
- 14. 如何識別iPhone屏幕
- 15. event.getX和屏幕座標
- 16. 圖像處理手特徵識別
- 17. 在屏幕上的特定物理座標中觸發觸摸事件Android
- 18. 取所有屏幕座標
- 19. Andengine縱向屏幕座標
- 20. JOGL屏幕座標關閉
- 21. 邏輯和屏幕座標
- 22. 檢查特定的手勢識別器
- 23. 從標準化設備座標到屏幕座標的關係?
- 24. 2D遊戲演員座標vs屏幕座標
- 25. 將十進制經度/緯度座標轉換爲iPad屏幕座標
- 26. 如何繪製屏幕上的緯度/經度座標?
- 27. 檢測屏幕上的座標
- 28. 使用CGAffineTransform的屏幕座標
- 29. Android MapView座標x屏幕位置
- 30. Cocos2D iPhone - 屏幕座標x精靈的內部座標
謝謝!我認爲必須有一些API調用,我只是在祖先樹上看起來不夠。關於機器人課的聽力是一個巨大的獎金;我不知道這樣的事情是否存在,而且你很有可能爲我節省了幾天的工作時間! – DVA 2010-04-29 02:08:04
@DVA感謝您的評論,良好的反饋和熱心是讓人們回答問題的原因。樂意效勞 :) – 2010-04-29 11:20:07