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!