Home > Java > javaTutorial > body text

Introducing two methods to achieve centered display of Java forms

黄舟
Release: 2017-07-17 09:56:48
Original
1405 people have browsed it

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);
Copy after login

The 2nd method:

setLocationRelativeTo(null);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template