在使用 Java 时,我发现启动应用程序时很难将主窗口定位在屏幕中央。
有什么办法可以做到吗? 它不必垂直居中,水平对齐对我来说是更重要的目标。但也欢迎垂直对齐。
最佳答案
当你传递一个 null
这个方法有一个特殊的效果。根据Javadoc:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
这应该在设置大小或调用 pack()
之后,但在设置为可见之前完成,如下所示:
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
关于java - 无论显示器分辨率如何,如何将 JFrame 设置为居中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2442599/