设置 ContentPane 的大小(在 JFrame 内部) [英] Setting the size of a ContentPane (inside of a JFrame)

查看:28
本文介绍了设置 ContentPane 的大小(在 JFrame 内部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置 JFrame 的大小,以便 contentPane 是所需的大小.JFrame.setSize() 没有考虑窗口装饰,所以 contentPane 有点太小了.窗口装饰的大小因平台和主题而异,因此尝试手动对其进行说明是个坏消息.

I want to set the size of a JFrame such that the contentPane is the desired size. JFrame.setSize() doesn't take the window decorations into account, so the contentPane is slightly too small. The size of the window decorations are platform and theme specific, so it's bad news to try to manually account for them.

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

JFrame.getContentPane().setSize() fails because it's managed.

想法?

谢谢!

推荐答案

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

In Java 5 and later this is the easiest method:

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

与往常一样,上述内容应在 EDT 中执行.

As always the above should be executed in the EDT.

这篇关于设置 ContentPane 的大小(在 JFrame 内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆