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

本文介绍了如何使用Java Swing库创建并居中显示一个JFrame窗口。通过两种方式实现:一是自定义位置;二是自动居中。对于居中显示的需求,无需手动调整坐标,只需调用`setLocationRelativeTo(null)`即可实现窗口在屏幕中的居中效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义位置
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值