问题描述
我将地图图像放置为JLabel
的图标.我正在使用以下代码来获取单击鼠标的位置的X,Y坐标.我已将此代码放在JLabel
的MouseClick事件中.
I have an image of a map placed as the icon of a JLabel
.I am using following code to get the X,Y coordinates of the location where the mouse is clicked.I have put this code in the MouseClick event of the JLabel
.
Point point = MouseInfo.getPointerInfo().getLocation();
double X = point.getX();
double Y = point.getY();
,但是坐标取决于JFrame
表单的位置.如果移动表格,则坐标会更改.
but the coordinates depend on the location of the JFrame
form. If the form is moved the coordinates change.
反正我可以冻结JFrame
吗?或者无论如何,我可以将图像的一个角设为0,0并获得相对于该角的其他坐标吗? (这样我就可以计算出实际坐标)
Is there anyway I can freeze the JFrame
?OrIs there anyway I can get a corner of the image as 0,0 and get the other coordinates relative to that? (So I can calculate the actual coordinates)
推荐答案
getLocation
返回相对于屏幕的鼠标坐标.改用MouseEvent
中的坐标
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
double x = e.getX();
double y = e.getY();
...
}
});
这篇关于如何在JLabel中获取图像的X,Y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!