无论显示器分辨率如何,如何将 JFrame 设置为居中显示?
在使用 Java 时,我发现启动应用程序时很难将主窗口定位在屏幕中央.
While working with Java, I find it hard to position my main window in the center of the screen when I start the application.
有什么办法可以做到吗?它不必垂直居中,水平对齐对我来说是更重要的目标.但也欢迎垂直对齐.
Is there any way I can do that? It doesn't have to be vertically centered, horizontal alignment is the more important goal for me. But vertical alignment is also welcome.
推荐答案
我一直都是这样:
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
其中 this
是所涉及的 JFrame.
where this
is the JFrame involved.
相关文章