A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王金科 中级黑马   /  2012-8-23 00:00  /  2215 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王金科 于 2012-8-23 15:39 编辑

如题,怎么设置Frame的窗口居中?

public class  MyMenuDemo
{
public static void main(String[] args)
{
  private Frame f;
f = new Frame("my window");
f.setBounds(300,100,650,600);//这里我怎么设置窗口居中啊?
f.setVisible(true);
}
}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

6 个回复

倒序浏览
本帖最后由 赵俊杰 于 2012-8-23 00:06 编辑

Dimension dem=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=(int)dem.getWidth(),screenHeight=(int)dem.getHeight();
int thisWidth=500,thisHeight=600;
int thisX=(screenWidth-thisWidth)/2,thisY=(screenHeight-thisHeight)/2;  //这句是关键。窗体X坐标=(屏幕宽度-窗体宽度)/2,Y坐标亦然。

this.setBounds(thisX,thisY,thisWidth,thisHeight);

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
赵俊杰 发表于 2012-8-23 00:02
Dimension dem=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=(int)dem.getWidth(),scree ...
  1. Dimension dem=Toolkit.getDefaultToolkit().getScreenSize();
  2. int screenWidth=(int)dem.getWidth(),screenHeight=(int)dem.getHeight();
复制代码
那么这两句就是获取屏幕长和宽了,原来这个我还没学到呢,呵呵,不过现在提现学到了.感谢哦
回复 使用道具 举报
用了你这个代码,果然居中 ,哈哈{:soso_e113:}
回复 使用道具 举报
  1. import java.awt.*;
  2. public class MyMenuDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Frame f;
  7.                 f = new Frame("my window");               
  8.                 f.setSize(650,600);
  9.                 Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
  10.                 f.setBounds(center.x-650/2,center.y-600/2,650,600);
  11.                 f.setVisible(true);
  12.         }
  13. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1

查看全部评分

回复 使用道具 举报
Frame f;

                f = new Frame("my window");               
                f.setSize(650,600);   //像这种数值问题,以后可以统一定义在上面,看时好看,改时也好改,加油。{:soso_e100:}
                Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
                f.setBounds(center.x-650/2,center.y-600/2,650,600);            
              f.setVisible(true);


回复 使用道具 举报
赵俊杰 发表于 2012-8-23 10:51
Frame f;

                f = new Frame("my window");               

OK,谢谢兄弟提醒,加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马