在实现之前获取JPanel的大小

发布于 2024-12-09 06:15:20 字数 144 浏览 3 评论 0原文

我已经创建了 JPanel 并已向其中添加了组件,我将将该 JPanel 传递给 PopUpFactory...那么我可以在传递 JPanel 之前获取 JPanel 的大小吗?

我将 Jlabel 放入其中,然后添加文本,但我不知道该文本的大小......

I've created JPanel and have already added components into it and I'm going to pass that JPanel to PopUpFactory... So can I get size of JPanel before passing it?

I put Jlabel into it and text after that and I don't know the size of that text...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本宫微胖 2024-12-16 06:15:20

您可以使用 setPreferredSize(Dimension);例如,

JPanel pnl = new JPanel(new BorderLayout());
pnl.setPreferredSize(new Dimension(640, 480));

该值随后可通过调用 getPreferredSize() 获得,并将在布局组件时使用,但请注意,不能保证它实际上会以此大小呈现。

为什么在渲染之前实际上需要尺寸?通常,使用 Swing 编程,您不需要处理显式的尺寸/大小,因为所选布局将为您处理这些细节。

编辑

为了解决OP关于JTextField的查询,这里的一个选项是调用基于int的构造函数,该构造函数接受预期的列数。这会导致文本字段呈现足够宽以支持该数量的字符。

我的第二点解决了 setXXXSize 方法永远不应该直接调用并且开发人员应该仅依赖于 LayoutManager 的评论。这并不总是合适的 - 通常有必要设置主应用程序框架的首选大小。例如,假设我正在 Swing 中编写一个简单的浏览器应用程序。主框架的大部分是用于呈现 HTML 的 JEditorPane。如果我没有为此组件(或包含框架)设置首选大小,并且调用 pack(),则框架可能会渲染得尽可能小,而不是具有合理的尺寸。

You can set the preferred size using setPreferredSize(Dimension); e.g.

JPanel pnl = new JPanel(new BorderLayout());
pnl.setPreferredSize(new Dimension(640, 480));

This value will subsequently be obtainable by calling getPreferredSize() and will be used when laying out the component, although note that it is not guaranteed that it will actually be rendered at this size.

Why do you actually require the size prior to rendering it? Typically with Swing programming you don't need to deal with explicit dimensions / sizes as the chosen layout will take care of these specifics for you.

EDIT

To address the OP's query regarding JTextField, one option here it to call the int based constructor that accepts the anticipate number of columns. This causes the text field to be rendered wide enough to support that number of characters.

My second point addresses the comment that the setXXXSize methods should never be called directly and that the developer should rely solely on the LayoutManager. This is not always appropriate - Typically it is necessary to set the preferred size of your main application frame. For example, suppose I were writing a a simple browser application in Swing. The majority of the main frame is a JEditorPane for rendering HTML. If I do not set a preferred size for this component (or the containing frame) and I call pack() the frame is likely to be rendered as small as possible, rather than with sensible dimensions.

心碎的声音 2024-12-16 06:15:20

如果 JComponents 不会返回 getSizegetLocationgetBoundsgetXxxSize,如果 < code>JComponents 以前在屏幕上或调用 pack() 之后不可见

,但为什么要关心这一点,因为使用了(正确且正确的)LayoutManager 可以自动执行此操作,这就是为什么 LayoutManager 存在那里,为什么要关心它

JComponents doesn't returns getSize, getLocation, getBounds or getXxxSize if a JComponents hasn't been previously visible on the screen or after call pack()

but why care about that, because usage of (proper and correct) LayoutManager can do that automatically, that reason why LayoutManager exist there, really why care about that

嘦怹 2024-12-16 06:15:20

只需调用 JLabel 的 getPreferredSize 方法即可。无论是否未实现 jlabel 的容器,如果您在设置 jlabel 可见之前设置 jlabel 的文本,则首选大小会发生变化。

Just call getPreferredSize method for JLabel.No matter if container of it is not realized, preferred size changes if you are setting text of jlabel even before you set it visible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文