我是一个.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);
}
上面的代码工作正常,但问题是我看到的形式从topleft最多到中心屏幕。我也尝试在formWindowOpened事件中添加该代码,并仍显示相同的操作。有更好的方法吗?就像在.NET应用程序中有一个中心屏幕位置。或者如果上面的代码是正确的,在什么事件我会把它?
感谢您阅读。