英文:
making x0 and y0 start from center in jframe?
问题
你好,我只想知道如何使x和y轴从jframe的中心开始,而不使用frame.setLocationRelativeTo(null)
,因为我希望能够使jframe跟随我的光标,目前它把jframe的左上角放在我的光标位置,但我希望它在中心位置。谢谢。
英文:
Hi I was just wondering how I am able to make the x and y axis start from the center of the jframe without using frame.setLocationRelativeTo(null)
because I want to be able to have the jframe follow my cursor, right now it is putting the jframe's top left corner to my cursor but i want it to be in the center.
Thanks.
答案1
得分: 1
这是一个相当简单的任务
int newX = cursorX - frame.getWidth() / 2;
int newY = cursorY - frame.getHeight() / 2;
然后
frame.setLocation(newX, newY);
这将使窗口定位在这样的方式下,鼠标光标位于其中间。
英文:
Its quite easy task
int newX=cursorX-frame.getWidth()/2;
int newY=cursorY-frame.getHeight()/2;
and than
frame.setLocation(newX,newY);
This will position your frame in a way that mouse cursor will be in the middle of it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论