public class ScreenCenter {
public static final int WIDTH = 5 ;
public static final int HEIGHT = 5 ;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Window window = new Window(null);
window.setBounds((getScreenWidth()-WIDTH)/2, (getScreenHeight()-HEIGHT)/2, WIDTH, HEIGHT);
window.setBackground(Color.red);
window.setVisible(true);
}
/**
* 得到屏幕的宽度
* @return
*/
public static int getScreenWidth(){
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
return (int)dim.getWidth();
}
/**
* 得到屏幕的高度
* @return
*/
public static int getScreenHeight(){
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
return (int)dim.getHeight();
}
}