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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2192291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:35:29  来源:igfitidea点击:

How to get screen position of a Swing element?

javaswing

提问by gotch4

how do I get the actual screen position of an element, say a Button? If I use getBounds I get the position relative to the parent container, not the screen...

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

采纳答案by bruno conde

回答by Bobby

You can try this:

你可以试试这个:

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