2 回答

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) 这个方法。

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的大小或者位置。
添加回答
举报