需要绝对定位时,空布局管理器的替代解决方案 [英] Alternative solution to null layout manager when absolute positioning is needed

查看:78
本文介绍了需要绝对定位时,空布局管理器的替代解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我读了这个Swing教程 http://zetcode.com/tutorials/javaswingtutorial/resizablecomponent /了解如何在Swing中实现可调整大小的组件.

Few months ago i read this Swing tutorial http://zetcode.com/tutorials/javaswingtutorial/resizablecomponent/ for understanding how implementing resizable components inside Swing.

该教程介绍了如何使用空LayoutManager来绝对定位子组件.

The tutorial tells about using a null LayoutManager in order to have the possibility of absolute positioning child components.

在与另一个主题相关的讨论中,@ Andrew Thompson指出使用空布局是一个坏主意.本文 http://download.oracle.com/javase/tutorial/uiswing /layout/none.html 也不希望使用null布局管理器,无论如何它表示在某些情况下这可能是有用的.

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.

在以下情况下:

  1. 我需要对JPanel的子组件进行绝对定位
  2. 我需要为子组件实现自己的边界框条件(以建立可能的重叠情况)
  3. 我需要调整JPanel的子组件的大小

在上述情况下使用空布局管理器是否正确?

Is correct in the described situation use a null layout manager?

是否还有其他更好的解决方案,可以使用JPanel容器组件的布局管理器来实现该目标?

Or there are other better solutions to achieve that goals using a layout manager for my JPanel container component?

预先感谢

推荐答案

您最好的解决方案是使用功能和控件来实现自己的LayoutManager(或扩展现有的,如果真的很接近您的需求).您正在寻找.您的绝对定位要求将完全成为布局管理器的一种方法.

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.

例如,我过去一直遵循扩展FlowLayout的技术,以使它能够正确包装包含在JScrollPane中的元素. 这里是一个示例,尽管这与我所提到的不一样使用.

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.

这篇关于需要绝对定位时,空布局管理器的替代解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆