首先获取由帧修剪的像素。
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);
这将使窗口在屏幕上居中。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)