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