Alternative solution to null layout manager when absolute positioning is needed

不羁岁月 提交于 2019-12-31 01:44:12

问题


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:

  1. i need absolute positioning for a JPanel's child components
  2. i need to implement my own bounding box conditions for child components (in order to establish possible overlapping situations )
  3. 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


回答1:


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 a JScrollPane. Here is an example, although this is not the same one that I used.



来源:https://stackoverflow.com/questions/5442520/alternative-solution-to-null-layout-manager-when-absolute-positioning-is-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!