无论显示器分辨率如何,如何将JFrame设置为居中显示? [英] How to set JFrame to appear centered, regardless of monitor resolution?

查看:138
本文介绍了无论显示器分辨率如何,如何将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.

推荐答案

我总是以这种方式做到:

I always did it in this way:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

其中是涉及的JFrame。

where this is the JFrame involved.

这篇关于无论显示器分辨率如何,如何将JFrame设置为居中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆