Java Swing如何让窗体居中显示

Java Swing如何让窗体居中显示

源码:

package com.himarking.tool;

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() {
        // 设置窗体可见
        this.setVisible(true);
        // 设置窗体位置和大小
        this.setBounds((width - windowsWedth) / 2,
                (height - windowsHeight) / 2, windowsWedth, windowsHeight);
    }
    
    public static void main(String[] args) {
        new AppWindows();
    }

}

结果如下图:

posted @ 2020-01-14 16:54  整合侠  阅读(1552)  评论(0编辑  收藏  举报