Java:JFrame.setLocationRelativeTo(null)没有使用OpenJDK 1.6.0_18将窗口置于Ubuntu 10.04 / gnome 2.30.2的中心 [英] Java: JFrame.setLocationRelativeTo(null) not centering the window on Ubuntu 10.04 / gnome 2.30.2 with OpenJDK 1.6.0_18

查看:1244
本文介绍了Java:JFrame.setLocationRelativeTo(null)没有使用OpenJDK 1.6.0_18将窗口置于Ubuntu 10.04 / gnome 2.30.2的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

    JFrame jFrame = new JFrame("Test");
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setLocationRelativeTo(null);
    jFrame.setSize(600, 600);
    jFrame.pack();
    // jFrame.setLocationRelativeTo(null); // same results
    jFrame.setVisible(true);

截图http://img193.imageshack.us/img193/5296/screenshotrev.png

这是OpenJDK的错吗?我记得听说它不如Sun的那么好,但是因为它成了Ubuntu的标准或者我决定接受它的任何东西。该程序可能会在Windows上运行,所以我想我将不得不在那里检查...任何简单的方法以平台无关的方式修复它而不破坏它已经工作的地方?

Is this the OpenJDK's fault? I recall hearing it wasn't as good as Sun's, but since it became the standard for Ubuntu or whatever I decided to go along with it. The program is probably gonna run on windows, so I suppose I'm gonna have to check there... Any easy way to fix this in a platform independent way without breaking it where it already works?

推荐答案

一种方法是手动定位窗口。在致电 pack()后立即输入以下代码。

One way is to manually position the window. Put the following code right after your call to pack().

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point middle = new Point(screenSize.width / 2, screenSize.height / 2);
Point newLocation = new Point(middle.x - (jFrame.getWidth() / 2), 
                              middle.y - (jFrame.getHeight() / 2));
jFrame.setLocation(newLocation);

免责声明,这只是在Windows上测试过。

此外,您应始终使用 setPreferredSize()而不是 setSize()

Also, you should always use setPreferredSize() instead of setSize().

这篇关于Java:JFrame.setLocationRelativeTo(null)没有使用OpenJDK 1.6.0_18将窗口置于Ubuntu 10.04 / gnome 2.30.2的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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