我是一个.Net开发人员,但不知何故,我的任务是在java中创建一个简单的应用程序,以解决某些额外的原因。我能够创建该应用程序,但我的问题是如何在应用程序启动时将屏幕居中显示在屏幕上?如何在中央屏幕上放置表格?
这里是我的代码:
private void formWindowActivated(java.awt.event.WindowEvent evt)
{
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Determine the new location of the window
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
// Move the window
this.setLocation(x, y);
}
上面的代码工作正常,但问题是我见过的形式从左上最移到屏幕中央。我还尝试在formWindowOpened
事件中添加该代码,并仍显示相同的操作。有没有更好的方法呢?就像在.NET Application
那里有一个CenterScreen Position
。或者,如果上面的代码是正确的,我会在什么事件?
感谢您阅读本文。
你在用什么? JFrame的?如果是这样,在将可见性设置为true之前,请先尝试设置位置。 – dpp 2012-03-03 04:04:44
@domanokz是先生。 – 2012-03-03 04:09:13
我在这里闻到Pinoy。有关JFrame的参考http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html。因为它是超类框架http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Frame.html。对于Frame的超类,Window http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Window.html ...在最后一个链接上,您将看到由属性setter继承的属性设置器Window类中的JFrame,setLocationRelativeTo。 – dpp 2012-03-03 04:19:28