关于JFrame的问题

DUCI_TTO 2006-03-20 09:12:10
怎么设置我JFrmae在屏幕上显示的位置,我的本意是要程序启动以后弹出的JFream显示在屏蔽的中央,但是它却显示在屏幕的左上角,我应该怎么设置,谢谢
...全文
87 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
believefym 2006-03-20
  • 打赏
  • 举报
回复
/**
* centerlize the component,that is locate the component in the
* center of the screen
* @param c the component to be centerlized
*/
public static void center(Component c) {
center(c, c.getSize());
}


/**
* centerlize the component,whose size is dimension me
* @param c the component to centerlized
* @param me dimension the component to be shown
*/
public static void center(Component c, Dimension me) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
c.setLocation(screenSize.width / 2 - me.width / 2, screenSize.height
/ 2 - me.height / 2);
}


/**
* make the component be in center of the parent component,
* if the component exceeds the borders of the screen,then
* make it in the screen
* @param parent the parent component
* @param me the component to be centerlized
*/
public static void center(Component parent,Component me){
Point p = parent.getLocation();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension psize = parent.getSize();
Dimension msize = me.getSize();
Point m = new Point((int)(p.getX()+(psize.width-msize.width)/2),
(int)(p.getY()+(psize.height-msize.height)/2));
if(m.x+msize.width > screen.width ){
m.x = screen.width-msize.width;
}

else if(m.x<0){
m.x=0;
}
if(m.y+msize.height > screen.height){
m.y = screen.height - msize.height;
}
else if(m.y<0){
m.y = 0;
}
me.setLocation(m.x,m.y);
}
Paripatetic 2006-03-20
  • 打赏
  • 举报
回复
setBounds(x1,y1,x2,y2);
前两个参数是位置
后两个是窗口大小

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧