admin管理员组

文章数量:1570219

1.  frame.setLocation()
setLocation

public void setLocation(int x,int y)

将组件移到新位置。通过此组件父级坐标空间中的 x 和 y 参数来指定新位置的左上角。
参数:
x - 父级坐标空间中新位置左上角的 x 坐标
y - 父级坐标空间中新位置左上角的 y 坐标

        因此,要使得窗口能够居中显示,代码如下:

//设置frame居于屏幕中央方式1
java.awt.Toolkit toolkit=java.awt.Toolkit.getDefaultToolkit();
Dimension screenSize=toolkit.getScreenSize();
double screenWidth=screenSize.getWidth();
double screenHeight=screenSize.getHeight();
frame.setLocation((int)(screenWidth-frame.getWidth())/2,(int)(screenHeight-frame.getHeight())/2);

2. setLocationRelativeTo()
setLocationRelativeTo
      public void setLocationRelativeTo(Component c)
设置窗口相对于指定组件的位置。
如果组件当前未显示,或者 c 为 null,则此窗口将置于屏幕的中央。中点可以使用 GraphicsEnvironment.getCenterPoint 确定。
如果该组件的底部在屏幕外,则将该窗口放置在 Component 最接近窗口中心的一侧。因此,如果 Component 在屏幕的右部,则Window 将被放置在左部,反之亦然。
参数:
c - 确定窗口位置涉及的组件
从以下版本开始:
1.4

这种方法使用简单,代码如下

//设置frame居于屏幕中央方式2
frame.setLocationRelativeTo(null);
http://blog.csdn/yahohi/article/details/7000290

本文标签: 屏幕framePC