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!