Java中GUI的展示在屏幕中间的方法

Java中GUI的显示在屏幕中间的方法
所需要的类:GraphicsEnvironment,Point。用到的方法有:GraphicsEnvironment类中的静态方法getLocalGraphicsEnvironment()方法、getCentPoint()方法。

首先,我们要创建一个GraphicsEnvironment类的对象。

GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvionment();

然后使用对象ge来获取屏幕的中心点:

Point p=ge.getCenterPoint();

然后调用GUI的setLocation方法来定义GUI的显示位置:

setLocation(p.x-XSize/2,p.y-YSize/2);

其中XSize表示GUI组件的宽,YSize表示GUI组件的高!