如何获取Swing元素的屏幕位置?

got*_*ch4 28 java swing

如何获取元素的实际屏幕位置,比如说按钮?如果我使用getBounds,我得到相对于父容器的位置,而不是屏幕......

Bob*_*bby 8

你可以试试这个:

// Convert a coordinate relative to a component's bounds to screen coordinates 
Point pt = new Point(component.getLocation()); 
SwingUtilities.convertPointToScreen(pt, component); 
Run Code Online (Sandbox Code Playgroud)