获取JFrame内容的实际大小

获取JFrame内容的实际大小,第1张

获取JFrame内容的实际大小

首先获取由帧修剪的像素。

int reqWidth = reqHeight = 750;// first set the sizeframe.setSize(reqWidth, reqHeight);// This is not the actual-sized frame. get the actual sizeDimension actualSize = frame.getContentPane().getSize();int extraW = reqWidth - actualSize.width;int extraH = reqHeight - actualSize.height;// Now set the size.frame.setSize(reqWidth + extraW, reqHeight + extraH);

另一种更简单的方法。以前的作品,但是建议这样做。

frame.getContentPane().setPreferredSize(750, 750);frame.pack();

希望这可以帮助。

编辑:

在将组件添加到框架之前,在构造函数中添加它。并设置在中间,使用

frame.setLocationRelativeTo(null);

这将使窗口在屏幕上居中



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5475833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存