java 在窗口中找到JFrame的位置/Position

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7950726/
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-10-30 22:09:35  来源:igfitidea点击:

Find the location/Position of JFrame in the window

javaswinglocationjframe

提问by nebula

I am trying to find the Starting location/position of the JFrame in order to map the mouse cursor from the JFrame window to desktop. Is there anyway to find the X,Y position of JFrame ?

我试图找到 JFrame 的起始位置/位置,以便将鼠标光标从 JFrame 窗口映射到桌面。有没有办法找到 JFrame 的 X,Y 位置?

回答by Harry Joy

You can use getLocation()or getLocationOnScreen()method of JFramewhich are inherited from java.awt.Component.

您可以使用继承自 java.awt.Component的JFramegetLocation()getLocationOnScreen()方法。

回答by Ajmal Muhammad P

you have to implement the MouseListener Interface and call the override methods

您必须实现 MouseListener 接口并调用覆盖方法

public void mouseEntered(MouseEvent me) 

and use

并使用

MouseEvent.getX();

MouseEvent.getXOnScreen();

回答by Berk KARAAL

Jframe.getX() // that will give you location of top left X coordinate of your jframe


Jframe.getY() // that will give you location of top left Y coordinate of your jframe