需要绝对定位时空布局管理器的替代解决方案
几个月前,我读了这个 Swing 教程 http://zetcode.com/tutorials/javaswingtutorial/resizingcomponent/ 了解如何在 Swing 中实现可调整大小的组件。
本教程讲述了如何使用 null LayoutManager 以便能够绝对定位子组件。
在与另一个主题相关的讨论中,@Andrew Thompson 指出使用空布局是一个坏主意。本文 http://download.oracle.com/javase/tutorial/uiswing /layout/none.html 将空布局管理器描述为不可取,无论如何它表示在某些情况下可能有用。
在以下情况下:
- 我需要 JPanel 的子组件的绝对定位
- 我需要为子组件实现我自己的边界框条件(以便建立可能的重叠情况)
- 我需要 JPanel 的子组件可调整大小
在所描述的情况下使用是正确的空布局管理器?
或者还有其他更好的解决方案可以使用 JPanel 容器组件的布局管理器来实现该目标?
提前致谢
Few months ago i read this Swing tutorial http://zetcode.com/tutorials/javaswingtutorial/resizablecomponent/ for understanding how implementing resizable components inside Swing.
The tutorial tells about using a null LayoutManager in order to have the possibility of absolute positioning child components.
In a discussion relative to another subject @Andrew Thompson pointed out that using null layout is a bad idea. This article http://download.oracle.com/javase/tutorial/uiswing/layout/none.html describe null layout manager as not desirable too, anyway it says that in certain situations the might be useful.
In a situation where:
- i need absolute positioning for a JPanel's child components
- i need to implement my own bounding box conditions for child components (in order to establish possible overlapping situations )
- i need JPanel's child component be resizable
Is correct in the described situation use a null layout manager?
Or there are other better solutions to achieve that goals using a layout manager for my JPanel container component?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最好的解决方案是使用您正在寻找的功能和控件来实现您自己的 LayoutManager(或者扩展现有的布局管理器,如果它确实符合您的需求)。您的绝对定位要求将简单地成为布局管理器的一种方法。
例如,我过去曾遵循扩展
FlowLayout
的技术,使其能够在包含在JScrollPane
中时正确包装元素。 这是一个示例,尽管这与我的示例不同用过的。Your best solution would be to implement your own
LayoutManager
(or extend an existing one, if one is really close to your needs) using the features and controls you are looking for. Your absolute positioning requirements would simply become a method of your layout manager.For instance, I have, in the past, followed techniques to extend
FlowLayout
to give it abilities to properly wrap elements when contained in aJScrollPane
. Here is an example, although this is not the same one that I used.