---
title: "窗体在显示器上居中显示方法"
author: "郭广川"
date: "2010-08-06T17:06:58Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-08-06T17:09:32Z"
id: 731159
url: "https://mzhx-com.iteye.com/blog/731159"
---

import java.awt.Toolkit;  
import javax.swing.JButton;

 

public class repaintttttt extends javax.swing.JFrame {

 

 public void shouUI() {  
  

// 设置窗体大小  
  int windowsWedth = 600;  
  int windowsHeight = 600;  
  

// 得到显示器屏幕的宽高  
  int width = Toolkit.getDefaultToolkit().getScreenSize().width;  
  int height = Toolkit.getDefaultToolkit().getScreenSize().height;

  

// 标题  
  this.setTitle("点击打开");  
    
  // 设置窗体不可改变大小  
  this.setResizable(false);  
    
 //添加流布局管理器  
  this.setLayout(new java.awt.FlowLayout());  
    
  

//添加按钮  
  this.add(new JButton("梅竹寒香"));

  

//设置窗体在显示器居中显示  
  this.setBounds((width - windowsWedth) / 2,  
    (height - windowsHeight) / 2, windowsWedth, windowsHeight);  
    
  //设置程序默认关闭方式  
  this.setDefaultCloseOperation(3);  
    
  //设置窗体可见  
  this.setVisible(true);  
 }

 

 public static void main(String args\[]) {  
  repaintttttt rp = new repaintttttt();  
  rp.shouUI();  
 }

}