在实现之前获取JPanel的大小
我已经创建了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
setPreferredSize(Dimension)
;例如,该值随后可通过调用 getPreferredSize() 获得,并将在布局组件时使用,但请注意,不能保证它实际上会以此大小呈现。
为什么在渲染之前实际上需要尺寸?通常,使用 Swing 编程,您不需要处理显式的尺寸/大小,因为所选布局将为您处理这些细节。
编辑
为了解决OP关于
JTextField
的查询,这里的一个选项是调用基于int
的构造函数,该构造函数接受预期的列数。这会导致文本字段呈现足够宽以支持该数量的字符。我的第二点解决了 setXXXSize 方法永远不应该直接调用并且开发人员应该仅依赖于 LayoutManager 的评论。这并不总是合适的 - 通常有必要设置主应用程序框架的首选大小。例如,假设我正在 Swing 中编写一个简单的浏览器应用程序。主框架的大部分是用于呈现 HTML 的
JEditorPane
。如果我没有为此组件(或包含框架)设置首选大小,并且调用pack()
,则框架可能会渲染得尽可能小,而不是具有合理的尺寸。You can set the preferred size using
setPreferredSize(Dimension)
; e.g.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 theint
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 aJEditorPane
for rendering HTML. If I do not set a preferred size for this component (or the containing frame) and I callpack()
the frame is likely to be rendered as small as possible, rather than with sensible dimensions.如果
JComponents
不会返回getSize
、getLocation
、getBounds
或getXxxSize
,如果 < code>JComponents 以前在屏幕上或调用pack()
之后不可见,但为什么要关心这一点,因为使用了(正确且正确的)LayoutManager 可以自动执行此操作,这就是为什么
LayoutManager 存在那里,为什么要关心它
JComponents
doesn't returnsgetSize
,getLocation
,getBounds
orgetXxxSize
if aJComponents
hasn't been previously visible on the screen or after callpack()
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只需调用 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.