java swing 窗口在屏幕中默认显示的位置 居中位置显示

自定义位置
public class Swing_demo2 {

    public static void main(String[] args) {
        JFrame jframe  =new JFrame("标题");
        jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
        jframe.setSize(300,300);
        jframe.setLocation(500,300);//在屏幕中设置显示的位置
        jframe.setVisible(true);   //显示

    }

}


居中位置

   那么居中位置要不要去慢慢手动尝试呢,显然不用

public class Swing_demo2 {

    public static void main(String[] args) {
        JFrame jframe  =new JFrame("标题");
        jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
        jframe.setSize(300,300);
        jframe.setLocationRelativeTo(null);//在屏幕中居中显示
        jframe.setVisible(true);   //显示

    }

}



鲜花会生锈,盐巴会腐烂
原文地址:https://www.cnblogs.com/hunterxing/p/9709306.html