首页 > 技术文章 > Java 获取窗口的宽、高

Satu 2018-10-22 11:49 原文

创建一个新窗口,通过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 }

 

推荐阅读