創建一個新窗口,通過getSize()獲取這個窗口的寬、高。
1 import javax.swing.JFrame; 2 3 public class WindowInTheMiddle extends JFrame { 4 5 6 public static void main(String[] args) { 7 WindowInTheMiddle window = new WindowInTheMiddle(); 8 window.launchFrame(); 9 10 // Width & Height 11 System.out.println(window.getSize().getWidth()); 12 System.out.println(window.getSize().getHeight()); 13 } 14 15 public void launchFrame() { 16 this.setTitle("Hello World! I'm coming!"); 17 this.setSize(500, 500); 18 this.setLocation(300, 300); 19 this.setVisible(true); 20 } 21 }