是否可以动态添加组件到BoxLayout中间
我正在构建一个小型 swing 应用程序,其中创建了内容窗格,其中 BoxLayout 与 Y 轴对齐,并向其中添加了另外 2 个 JPanel。到目前为止,一切都很好。
现在我偶然发现了一个问题,我必须重新创建第一个 JPanel 并将其添加到其他两个 JPanel 的中间。
可能会不止一次将另一个面板添加到布局中,因此我希望它能够适当扩展。
结论:我正在寻找的是将组件添加到 BoxLayout 中,并可以选择将它们添加到最后一个组件之前。
谢谢。
I'm building a small swing application where I've created contentpane with BoxLayout aligned to Y axis and added another 2 JPanels to it. So far so good.
Now I've stumbled upon a problem where I have to re-create the first JPanel and add it to the middle of other two JPanels.
There might be more than one occurrence where another panel is added to the layout so I wish it would expand appropriately.
Conclusion: What I'm looking for is to add components to the BoxLayout with option to add them before the last component.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看
Container
API。add(...)
方法具有重载方法,允许您指定组件在容器中的位置。然后revalidate()
和repaint()
面板。Check out the
Container
API. Theadd(...)
method has overloaded methods that allow you to specify the position of the component in the container. Thenrevalidate()
andrepaint()
the panel.我只需删除所有组件,然后按照所需的顺序重新添加它们,确保在使用 BoxLayout 的容器上调用
revalidate()
和repaint()
。已经完成了这个动作。如果您需要 GUI 本身更改大小,那么您可能必须在保存这些组件的顶级窗口上调用
pack()
。I would simply remove all components and then re-add them in the order desired being sure to call
revalidate()
andrepaint()
on the BoxLayout-using container after you've completed this action.If you need your GUI itself to change size, then you may have to call
pack()
on the top-level window that holds these components.