67,538
社区成员
发帖
与我相关
我的任务
分享GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();setLocationRelativeTo(null);import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
private final int INIT_W = 600; //窗体初始宽度
private final int INIT_H = 460; //窗体初始高度
public MyFrame() {
super("Center Frame Test");
Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setBounds(p.x - INIT_W / 2, p.y - INIT_H / 2, INIT_W, INIT_H);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}
import javax.swing.*;
public class Test extends JFrame{
public Test() {
setSize(200, 200);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args){
new Test();
}
}