为了账号安全,请及时绑定邮箱和手机立即绑定

不管我怎么设置窗体就是不在中间出来,怎么做啊?为什么?

不管我怎么设置窗体就是不在中间出来,怎么做啊?为什么?

慕斯709654 2022-10-13 15:11:37
package com.lovo.bean;import java.awt.Container;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JFrame;public class MyFrame extends JFrame {private Container contentP;public MyFrame(){//this.addcontent();Toolkit tk = Toolkit.getDefaultToolkit(); //获取默认工具包Image img = tk.createImage("hp.JPG"); //生成图形this.setIconImage(img); //获取图像this.setSize(600, 400);// int w = (tk.getScreenSize().width - 600)/2;// int h = (tk.getScreenSize().height - 400)/2;// this.setLocation(w, h);this.setLocationRelativeTo(null);this.setTitle("HiLo Game");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}
查看完整描述

2 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

把// 这个删掉啊,你注释了,当然就。。。
Dimension p = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((p.width - 600) / 2, (p.height - 400) / 2);
我平常也是这样写的,没有问题,可以居中显示。

Dimension p = Toolkit.getDefaultToolkit().getScreenSize();
如果实在不行,试试 this.setBounds((p.width - 600) / 2, p.height - 400) / 2, 600, 400) 这个方法。

查看完整回答
反对 回复 2022-10-16
?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

把this.setLocationRelativeTo(null);去掉就可以了。
如下:

public MyFrame() {
// this.addcontent();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取默认工具包
Image img = tk.createImage("hp.JPG"); // 生成图形
this.setIconImage(img); // 获取图像
this.setSize(600, 400);
int w = (tk.getScreenSize().width - 600)/2;
int h = (tk.getScreenSize().height - 400)/2;
this.setLocation(w, h);
//this.setLocationRelativeTo(null);
this.setTitle("HiLo Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

如果还不行,就检查调用者Frame的代码。可能在调用Frame的地方再一次的设置了Frame的大小或者位置。


查看完整回答
反对 回复 2022-10-16
  • 2 回答
  • 0 关注
  • 255 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号