Swing 设置ContentPane的大小(JFrame内部)

Swing 设置ContentPane的大小(JFrame内部),swing,Swing,我想设置JFrame的大小,以便contentPane是所需的大小。setSize()没有考虑窗口装饰,因此contentPane稍微太小。窗口装饰的大小是特定于平台和主题的,因此尝试手动解释它们是个坏消息 JFrame.getContentPane().setSize()失败,因为它是托管的 想法 谢谢 在Java 5及更高版本中,这是最简单的方法: JFrame frame = new JFrame("Content Pane Size Example"); frame.getContent

我想设置JFrame的大小,以便contentPane是所需的大小。setSize()没有考虑窗口装饰,因此contentPane稍微太小。窗口装饰的大小是特定于平台和主题的,因此尝试手动解释它们是个坏消息

JFrame.getContentPane().setSize()失败,因为它是托管的

想法


谢谢

在Java 5及更高版本中,这是最简单的方法:

JFrame frame = new JFrame("Content Pane Size Example");
frame.getContentPane().setPreferredSize(new Dimension(400, 300));
frame.pack();
frame.setVisible(true);

如往常一样,上述内容应在EDT中执行。

如果您想自己执行,则在您的帧类型中:

this.setLayour(null);
this.pack();
Insets insets = this.getInsets();
this.setBounds(0,0, insets.left + width + insets.right, insets.top + height + insets.bottom);
this.setLocationRelativeTo(null);
这个问题可能有助于: