Home  >  Article  >  Java  >  Introducing two methods to achieve centered display of Java forms

Introducing two methods to achieve centered display of Java forms

黄舟
黄舟Original
2017-07-17 09:56:481390browse

The editor below will bring you two methods of centering a Java form (Example explanation). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

The 1st method:

//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);

The 2nd method:

setLocationRelativeTo(null);

The above is the detailed content of Introducing two methods to achieve centered display of Java forms. 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