我希望我的 jframe 在人的显示器中央打开。
(默认情况下,jframe 将在坐标处打开。(0,0))
为了实现这一点,在将框架设置为可见之前,我使用了此方法。
this.setLocation(x,y);
理论上,显示器屏幕尺寸可能不同,这意味着几乎所有计算机的中心坐标都会不同。
这是我的问题: 如何获取运行 swing 应用程序的计算机显示器的中心坐标?
最佳答案
How would I get the center coordinate of the computer monitor running the swing application?
您可以通过 java.awt.Toolkit
获取中心坐标:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.width/2;
int centerY = screenSize.height/2;
但是,您不需要它。
只需使用:
yourFrame.setLocationRelativeTo(null);
并且会自动居中
关于java - 如何用swing获取桌面的中心x和y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22416647/