Home  >  Article  >  Java  >  Example analysis of two methods in Java to realize centered display of form

Example analysis of two methods in Java to realize centered display of form

黄舟
黄舟Original
2017-07-24 14:31:351347browse

Method 1:

//setSize(300, 200);
pack();
// 得到显示器屏幕的宽、高
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 得到窗体的宽、高
int windowsWidth = this.getWidth();
int windowsHeight = this.getHeight();
//System.out.println(windowsWidth+","+windowsHeight);
        this.setBounds((width - windowsWidth) / 2,(height - windowsHeight) / 2, windowsWidth, windowsHeight);

Method 2:

setLocationRelativeTo(null);


The above is the detailed content of Example analysis of two methods in Java to realize centered display of form. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn