java swing 窗口大小,如何在java swing中设置特定的窗口(框架)大小?

这篇博客讨论了在Java GUI编程中遇到的问题:JFrame窗口显示过小。作者指出同时使用`frame.setSize()`和`frame.pack()`会导致窗口大小异常。解决方法是只保留一种方法,要么指定窗口尺寸,要么让`frame.pack()`根据组件自动调整大小。移除`frame.pack()`或将其放在设置尺寸之前可以修复问题。
摘要由CSDN通过智能技术生成

My code does not work:

JFrame frame = new JFrame("mull");

mull panel = new mull();

// add panel to the center of window

frame.getContentPane().add("Center", panel);

frame.setSize(500, 300); // << not working!!!

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack(); // give a suitable size to window automatically

frame.setVisible(true); // make window visible

I am getting very small window. How to fix it?

解决方案

Well, you are using both frame.setSize() and frame.pack().

You should use one of them at one time.

Using setSize() you can give the size of frame you want but if you use pack(), it will automatically change the size of the frames according to the size of components in it. It will not consider the size you have mentioned earlier.

Try removing frame.pack() from your code or putting it before setting size and then run it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值