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

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);
}

}

慕斯709654
浏览 256回答 2
2回答

慕姐4208626

把// 这个删掉啊,你注释了,当然就。。。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) 这个方法。

翻翻过去那场雪

把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的大小或者位置。
打开App,查看更多内容
随时随地看视频慕课网APP