Java 创建一个窗口,使其启动时位于屏幕中间

 

 1 import java.awt.Toolkit;
 2 
 3 import javax.swing.JFrame;
 4 
 5 public class WindowInTheMiddle extends JFrame {
 6     public static void main(String[] args) {
 7         WindowInTheMiddle window = new WindowInTheMiddle();
 8         window.launchFrame();
 9     }
10     
11     public void launchFrame() {
12         // The width & Height of the window
13         final int formWidth = 500;
14         final int formHeight = 500;
15         // The width & Height of the screen
16         int screenWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
17         int screenHeight = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
18         
19         // Set title, size, location & visible
20         this.setTitle("Hello World! I'm coming!");
21         this.setSize(formWidth, formHeight);
22         this.setLocation((screenWidth-formWidth)/2, (screenHeight-formHeight)/2);
23         this.setVisible(true);
24     }
25 }

 

posted @ 2018-10-22 13:59  heismk  阅读(976)  评论(0编辑  收藏  举报