How to set JFrame to appear centered, regardless of monitor resolution?(无论显示器分辨率如何,如何将 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.
这篇关于无论显示器分辨率如何,如何将 JFrame 设置为居中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无论显示器分辨率如何,如何将 JFrame 设置为居中显示?


- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 转换 ldap 日期 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01