本文實例講述了Java Swing實現(xiàn)讓窗體居中顯示的方法。分享給大家供大家參考,具體如下:
Swing組件是AWT組建的增強組件,是功能強大的“輕量級組件”。這里來簡單介紹一下Swing實現(xiàn)窗體居中顯示的方法。
其他不多說,直接上代碼!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package awtDemo; import java.awt.Toolkit; import javax.swing.JFrame; @SuppressWarnings ( "serial" ) public class AppWindows extends JFrame { // 得到顯示器屏幕的寬高 public int width = Toolkit.getDefaultToolkit().getScreenSize().width; public int height = Toolkit.getDefaultToolkit().getScreenSize().height; // 定義窗體的寬高 public int windowsWedth = 600 ; public int windowsHeight = 600 ; public AppWindows() { // 設(shè)置窗體可見 this .setVisible( true ); // 設(shè)置窗體位置和大小 this .setBounds((width - windowsWedth) / 2 , (height - windowsHeight) / 2 , windowsWedth, windowsHeight); this .setTitle( "www.ythuaji.com.cn - 窗體居中顯示效果" ); //設(shè)置可關(guān)閉 this .setDefaultCloseOperation(HIDE_ON_CLOSE); } public static void main(String[] args) { new AppWindows(); } } |
運行效果圖如下:
另外,代碼中關(guān)于setDefaultCloseOperation方法的使用可參考JFrame框架類中setDefaultCloseOperation的參數(shù)含義與用法
希望本文所述對大家java程序設(shè)計有所幫助。
原文鏈接:https://www.cnblogs.com/linfenghp/p/5948061.html