我应该避免在 Java Swing 中使用 set(Preferred|Maximum|Minimum) 大小方法吗?
有几次我因为建议使用以下方法而受到批评:
setPreferredSize()
setMinimumSize()
setMaximumSize()
on Swing
组件。当我想定义显示组件之间的比例时,我没有看到任何替代方法。有人告诉我:
对于布局,答案总是相同的:使用合适的LayoutManager。< /p>
我在网上搜索过,但没有找到对该主题的全面分析。所以我有以下问题:
- 我应该完全避免使用这些方法吗?
- 定义这些方法是有原因的。那么我应该什么时候使用它们呢?在什么情况下?出于什么目的?
- 使用这些方法到底会产生哪些负面后果? (我只能考虑在不同屏幕分辨率的系统之间添加可移植性)。
- 我不认为任何 LayoutManager 都能完全满足所有所需的布局需求。我真的需要为布局上的每一个小变化实现一个新的 LayoutManager 吗?
- 如果4的答案是“是”,这是否会导致LayoutManager类激增而变得难以维护?
- 在我需要定义组件的子级之间的比例的情况下(例如,child1 应使用 10% 的空间,child2 40%,child3 50%),是否可以在不实现自定义 LayoutManager 的情况下实现这一目标?
Several times I've been criticized for having suggested the use of the following methods:
setPreferredSize()
setMinimumSize()
setMaximumSize()
on Swing
components. I don't see any alternatives to their use when I want to define proportions between displayed components. I have been told this:
With layouts the answer is always the same: use a suitable LayoutManager.
I have searched the web, but I haven't found any comprehensive analysis of the subject. So I have the following questions:
- Should I completely avoid the use of those methods?
- The methods have been defined for a reason. So when should I use them? In which context? For what purposes?
- What exactly are the negative consequences of using those methods? (I can only think adding portability between systems with different screen resolution).
- I don't think any LayoutManager can exactly satisfy all desired layout needs. Do I really need to implement a new LayoutManager for every little variation on my layout ?
- If the answer to 4 is "yes", won't this lead to a proliferation of LayoutManager classes which will become difficult to maintain?
- In a situation where I need to define proportions between children of a Component (eg, child1 should use 10% of space, child2 40% ,child3 50%), is it possible to achieve that without implementing a custom LayoutManager?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我应该完全避免使用这些方法吗?
对于应用程序代码是的。
这些方法的定义是有原因的。那么我应该什么时候使用它们呢?在什么情况下?出于什么目的?
我不知道,我个人认为这是一个 API 设计意外。对子尺寸有特殊想法的复合组件有点强迫。 “稍微”,因为他们应该使用自定义的 LayoutManager 来实现他们的需求。
使用这些方法到底会产生哪些负面后果? (我只能考虑在不同屏幕分辨率的系统之间添加可移植性。)
中提到了一些技术原因(不完整,不幸的是,由于 SwingLabs 迁移到 java.net,链接已损坏) href="http://web.archive.org/web/20110614145256/http://wiki.java.net/twiki/bin/view/Javadesktop/SwingLabsImperialRules?TWIKISID=e1a6667476691b56753dc9b0744828c6#Do_not_use_component_setXXSize" rel="noreferrer">规则(呵呵) 或在链接 @bendicott 在他/她对 我的答案。在社交方面,给你不幸的同事带来大量的工作,他必须维护代码并追踪损坏的布局。
我认为没有任何 LayoutManager 能够完全满足所有所需的布局需求。我真的需要为布局上的每一个小变化实现一个新的 LayoutManager 吗?
是的,有足够强大的布局管理器来满足“所有布局需求”的非常好的近似。三大布局是 JGoodies FormLayout、MigLayout、DesignGridLayout。所以不,在实践中,除了简单的高度专业化的环境之外,您很少编写 LayoutManager。
如果4的答案是“是”,这是否会导致LayoutManager类激增而变得难以维护?
(4的答案是“否”。)
Yes for application code.
I don't know, personally I think of it as an API design accident. Slightly forced by compound components having special ideas about child sizes. "Slightly", because they should have implemented their needs with a custom LayoutManager.
Some (incomplete, and unfortunately the links are broken due to migration of SwingLabs to java.net) technical reasons are for instance mentioned in the Rules (hehe) or in the link @bendicott found in his/her comment to my answer. Socially, posing tons of work onto your unfortunate fellow who has to maintain the code and has to track down a broken layout.
Yes, there are LayoutManagers powerful enough to satisfy a very good approximation to "all layout needs". The big three are JGoodies FormLayout, MigLayout, DesignGridLayout. So no, in practice, you rarely write LayoutManagers except for simple highly specialized environments.
(The answer to 4 is "no".)
Any of the Big-Three can, can't even GridBag (never bothered to really master, too much trouble for too little power).
一些启发:
当您确实想要覆盖
get[Preferred|Maximum|Minimum]Size()<时,不要使用
set[Preferred|Maximum|Minimum]Size()
/code>,就像创建您自己的组件时可能完成的那样,如图 此处。当您可以依赖组件仔细重写的
getPreferred|Maximum|Minimum]Size
时,请勿使用set[Preferred|Maximum|Minimum]Size()
,例如显示此处及下方。请使用
set[Preferred|Maximum|Minimum]Size()
来派生后validate()
几何图形,如下所示和此处。如果组件没有首选大小,例如
JDesktopPane
,您可能必须在调用pack()
之后调整容器的大小,但是任何这样的选择都是任意的。评论可能有助于澄清意图。当您发现必须循环访问许多组件才能获取派生尺寸时,请考虑备用或自定义布局,如这些 评论。
A few heuristics:
Don't use
set[Preferred|Maximum|Minimum]Size()
when you really mean to overrideget[Preferred|Maximum|Minimum]Size()
, as might be done in creating your own component, shown here.Don't use
set[Preferred|Maximum|Minimum]Size()
when you could rely on a component's carefully overriddengetPreferred|Maximum|Minimum]Size
, as shown here and below.Do use
set[Preferred|Maximum|Minimum]Size()
to derive post-validate()
geometry, as shown below and here.If a component has no preferred size, e.g.
JDesktopPane
, you may have to size the container, after invokingpack()
, but any such choice is arbitrary. A comment may help clarify the intent.Consider alternate or custom layouts when you find that you would have to loop through many components to obtain derived sizes, as mentioned in these comments.
不,没有正式证据表明不允许调用或重写这些方法。事实上,Oracle 表示这些方法用于提供大小提示: http ://docs.oracle.com/javase/tutorial/uiswing/layout/using.html#sizealignment。
当扩展 Swing 组件(而不是调用自定义组件实例上的方法)时,它们也可能被覆盖(这是 Swing 的最佳实践)。
最重要的是,无论您如何指定组件的大小,确保组件的容器使用符合组件请求大小的布局管理器。
当您需要向容器布局管理器提供自定义尺寸提示以便组件能够良好布局时
许多布局管理器不关注组件请求的最大尺寸。但是,
BoxLayout
和SpringLayout
可以。此外,GroupLayout
提供了显式设置最小、首选或最大尺寸的功能,而无需接触组件。确保您确实需要设置组件的确切大小。每个 Swing 组件都有不同的首选大小,具体取决于它使用的字体以及外观和感觉。因此,设置大小可能会在不同系统上产生不同的 UI 外观
有时,
GridBagLayout
和文本字段可能会遇到问题,其中如果容器的大小小于首选大小,则使用最小大小,这可能会导致文本字段大幅缩小。JFrame
不强制覆盖getMinimumSize()
仅在其作品上调用setMinimumSize(..)
如果“实现”意味着“使用”,那么是的。没有一个
LayoutManger
可以处理所有事情,每个LayoutManger
都有其优点和缺点,因此每个都可以一起使用来生成最终布局。参考:
No, there is no formal evidence to suggest calling or overriding these methods is not allowed. In fact, Oracle says these methods are used for giving size hints: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html#sizealignment.
They may also be overridden (which is the best practice for Swing) when extending a Swing component (rather than calling the method on the custom component instance)
Most importantly no matter how you specify your component's size, be sure that your component's container uses a layout manager that respects the requested size of the component.
When you need to provide customized size hints to the containers Layout manager so that the component will be laid out well
Many layout managers do not pay attention to a component's requested maximum size. However,
BoxLayout
andSpringLayout
do. Furthermore,GroupLayout
provides the ability to set the minimum, preferred or maximum size explicitly, without touching the component.Make sure that you really need to set the component's exact size. Each Swing component has a different preferred size, depending on the font it uses and the look and feel. Thus having a set size might produce varied looks of the UI on different Systems
sometimes problems can be encountered with
GridBagLayout
and text fields, wherein if the size of the container is smaller than the preferred size, the minimum size gets used, which can cause text fields to shrink quite substantially.JFrame
does not enforce overridengetMinimumSize()
only callingsetMinimumSize(..)
on its worksIf by implementing you mean using then yes. Not one
LayoutManger
can handle everything, eachLayoutManager
has its pros and cons thus each can be used together to produce the final layout.Reference:
这里有很多很好的答案,但我想补充一点关于您通常应该避免这些问题的原因(这个问题刚刚在重复的主题中再次出现):
除了少数例外,如果当您使用这些方法时,您可能正在微调 GUI,使其在特定的外观和感觉上看起来不错(并且使用您的系统特定设置,例如您喜欢的桌面字体等)。这些方法本身并不是邪恶的,但使用它们的典型原因。一旦您开始调整布局中的像素位置和大小,您的 GUI 在其他平台上就会面临崩溃(或者至少看起来很糟糕)的风险。
作为一个例子,尝试更改应用程序的默认外观。即使仅使用平台上可用的选项,您也可能会对呈现的结果如此糟糕感到惊讶。
因此,以保持 GUI 在所有平台上功能齐全且美观的名义(请记住,Java 的主要优点之一是它的跨平台性),您应该依靠布局管理器等来自动调整 GUI 的大小您的组件,以便它在您的特定开发环境之外正确呈现。
综上所述,您当然可以想象这些方法是合理的情况。同样,它们本质上并不是邪恶的,但它们的使用通常是一个大危险信号,表明潜在的 GUI 问题。只要确保您意识到如果/当您使用它们时,很可能会出现并发症,并且始终尝试思考是否有另一种外观和感觉独立的解决方案来解决您的问题 - 通常您会发现这些方法不是必需的。
顺便说一句,如果您发现自己对标准布局管理器感到沮丧,有很多很好的免费开源第三方布局管理器,例如 JGoodies 的
FormLayout
或MigLayout
。一些 GUI 构建器甚至内置了对第三方布局管理器的支持 - 例如,Eclipse 的 WindowBuilder GUI 编辑器就附带了对FormLayout
和MigLayout
的支持。There are a lot of good answers here but I want to add a little more about the reasons why you should normally avoid these (the question just came up again in a duplicate topic):
With few exceptions, if you are using these methods you are probably fine-tuning your GUI to look good on a specific look-and-feel (and with your system-specific settings, e.g. your preferred desktop font, etc.). The methods themselves aren't inherently evil, but the typical reasons for using them are. As soon as you start tuning pixel positions and sizes in a layout you run the risk of your GUI breaking (or at minimum, looking bad), on other platforms.
As an example of this, try changing your application's default look-and-feel. Even just with the options available on your platform, you may be surprised at how poorly the results can be rendered.
So, in the name of keeping your GUI functional and nice-looking on all platforms (remember, one of the major benefits of Java is its cross-platformness), you should rely on layout managers, etc., to automatically adjust the sizes of your components so that it renders correctly outside of your specific development environment.
All that said, you can certainly conceive of situations where these methods are justified. Again, they aren't inherently evil, but their usage is normally a big red flag indicating potential GUI issues. Just make sure you are aware of the high potential for complications if/when you use them, and always try and think if there is another look-and-feel-independent solution to your problems -- more often than not you will find that these methods are not necessary.
By the way, if you find yourself getting frustrated with standard layout managers, there are a lot of good free, open-source third-party ones, for example JGoodies'
FormLayout
, orMigLayout
. Some GUI builders even have built-in support for third-party layout managers -- Eclipse's WindowBuilder GUI editor, for example, ships with support forFormLayout
andMigLayout
.如果您在 Java Swing 中的布局方面遇到问题,那么我强烈推荐 JGoodies
FormLayout
,它是 Karsten Lentzsch 此处。这个非常流行的布局管理器非常灵活,允许开发非常精美的 Java UI。
您可以在此处找到 Karsten 的文档,并且eclipse 中的一些相当好的文档 此处。
If you are having trouble with layouts in Java Swing, then I can highly recommend the JGoodies
FormLayout
provided freely as part of the Forms freeware library by Karsten Lentzsch here.This very popular layout manager is extremely flexible, allowing for very polished Java UIs to be developed.
You'll find Karsten's documentation in here, and some rather good documentation from eclipse here.
大多数人对这些方法知之甚少。你绝对不应该忽视这些方法。布局管理器是否遵守这些方法取决于布局管理器。此页面有一个表格,显示哪些布局管理器遵循其中哪些方法:
http://thebadprogrammer.com/swing-layout -manager-sizing/
我编写 Swing 代码已有 8 年多了,JDK 中包含的布局管理器始终满足我的需求。我从来不需要第三方布局管理器来实现我的布局。
我想说的是,除非您确定需要它们,否则您不应该尝试使用这些方法向布局管理器提供提示。在不给出任何尺寸提示的情况下进行布局(即让布局管理器完成其工作),然后您可以根据需要进行较小的更正。
These methods are poorly understood by most people. You should absolutely not ignore these methods. It is up to the layout manager if they honor these methods. This page has a table that shows which layout managers honor which of those methods:
http://thebadprogrammer.com/swing-layout-manager-sizing/
I have been writing Swing code for 8+ years and the layout managers included in the JDK have always served my needs. I have never needed a 3rd party layout manager to achieve my layouts.
I will say that you shouldn't try to give the layout manager hints with these methods until you are sure you need them. Do your layout without giving any sizing hints (i.e. let the layout manager do its job) and then you can make minor corrections if you need to.
也许 GridBagLayout 能满足您的需求。除此之外,网络上有大量的布局管理器,我敢打赌一定有一个适合您的要求。
Maybe
GridBagLayout
would satisfy your needs. Besides that, there's a ton of layout managers on the web, and I bet there's one that fits your requirements.我认为它与公认的答案不同。
1)我应该完全避免使用这些方法吗?
千万不要回避!它们的作用是向布局管理器表达组件的大小限制。如果您不使用任何布局管理器并尝试自行管理视觉布局,则可以避免使用它们。
不幸的是,Swing 没有提供合理的默认尺寸。然而,与其设置组件的尺寸,不如以 OOP 方式使用合理的默认值来降低您自己的组件的尺寸。 (在这种情况下,您可以在后代类中调用 setXXX。)或者,您可以重写 getXXX 方法以获得相同的效果。
2) 这些方法的定义是有原因的。那么我应该什么时候使用它们呢?在什么情况下?出于什么目的?
总是。创建组件时,根据该组件的用途设置其实际的最小/首选/最大尺寸。例如,如果您有一个用于输入国家/地区符号(例如英国)的 JTextField,则其首选大小应尽可能宽以容纳两个字符(使用当前字体等),但让它变得更大可能是没有意义的。毕竟,国家符号是两个字符。
相反,如果您有一个 JTextField 用于输入例如客户名称,它可以有一个首选大小,例如 20 个字符的像素大小,但如果调整布局大小,它可能会变得更大,因此将最大大小设置为更大。同时,拥有 0px 宽的 JTextField 是没有意义的,因此设置一个实际的最小大小(我想说的是 2 个字符的像素大小)。
3)使用这些方法到底会产生哪些负面后果?
(我只能考虑在不同屏幕分辨率的系统之间添加可移植性)。
没有负面后果。这些是布局管理器的提示。
4)我认为没有任何LayoutManager能够完全满足所有所需的布局需求。
我真的需要为布局上的每一个小变化实现一个新的 LayoutManager 吗?
不,绝对不是。通常的方法是级联不同的基本布局管理器,例如水平和垂直布局。
例如,下面的布局:
有两个部分。左右部分为横向布局。右侧部分是添加到水平布局的 JPanel,该 JPanel 具有垂直布局,垂直布局按钮。
当然,这对于现实生活中的布局来说可能会变得很棘手。因此,如果您要开发任何严肃的东西,基于网格的布局管理器(例如 MigLayout)会更好。
5)如果4的答案是“是”,这是否会导致LayoutManager类激增而变得难以维护?
不,你绝对不应该开发布局管理器,除非你需要一些非常特殊的东西。
6)在我需要定义
组件的子级之间的比例的情况下(例如,child1 应使用 10% 的空间,child2 40%,child3 50%),是否可以在不实现自定义 LayoutManager 的情况下实现这一目标?
基本上,一旦正确设置了首选尺寸,您可能就不想按百分比做任何事情。简单地说,因为百分比是没有意义的(例如,JTextField 为窗口大小的 10% 是没有意义的 - 因为可以缩小窗口以使 JTextField 变为 0px 宽,或者可以扩展窗口以使 JTextField 跨过一台显示器上的两个显示器)多显示器设置)。
但是,有时您可以使用百分比来控制 GUI 的较大构建块(例如面板)的大小。
您可以使用 JSplitPane 来预先设置两侧的比例。或者,您可以使用 MigLayout,它允许您以百分比、像素和其他单位设置此类约束。
I am seeing it differenty than the accepted answer.
1) Should I completely avoid the use of those methods?
Never avoid! They're there to express the size constraints of your components to the layout manager. You can avoid using them if you're not using any layout manager and try to manage the visual layout on your own.
Unfortunately, Swing is not coming with reasonable default dimensions. However, instead of setting the dimensions of a component, it is better OOP to descend your own component with reasonable defaults. (In that case you call setXXX in your descendant class.) Alternatively, you can override the getXXX methods for the same effect.
2) The methods have been defined for a reason. So when should I use them? In which context? For what purposes?
Always. When you create a component, set its realistic min/preferred/max size according to the use of that component. For example, if you have a JTextField for entering country symbols such as UK, its preferred size shall be as wide to fit two chars (with the current font, etc.) but probably it is meaningless to let it grow any bigger. After all, country symbols are two chars.
As opposite, if you have a JTextField for entering e.g. a customer name, it can have a preferred size for like the pixel size for 20 chars, but can grow to bigger if the layout is resized, so set the maximum size to more. At the same time, having a 0px wide JTextField is pointless, so set a realistic minimum size (I would say the pixel size of 2 chars).
3) What exactly are the negative consequences of using those methods?
(I can only think adding portability between systems with different screen resolution).
No negative consequences. These are hints for the layout manager.
4) I don't think any LayoutManager can exactly satisfy all desired layout needs.
Do I really need to implement a new LayoutManager for every little variation on my layout ?
No, definitely not. The usual approach is to cascade different basic layoutmanagers such as horizontal and vertical layout.
For example, the layout below:
is having two parts. The left and right parts are a horizontal layout. The right part is a JPanel added to the horizontal layout, and this JPanel is having a vertical layout which lays out the buttons vertically.
Of course, this can grow tricky with a real life layout. Therefore grid-based layout managers such as MigLayout are much better if you're about to develop anything serious.
5) If the answer to 4 is "yes", won't this lead to a proliferation of LayoutManager classes which will become difficult to maintain?
No, you definitely shall not develop layout managers, unless you need something very special.
6) In a situation where I need to define proportions...
between children of a Component (eg, child1 should use 10% of space, child2 40% ,child3 50%), is it possible to achieve that without implementing a custom LayoutManager?
Basically, once the preferred sizes are set right, you may not want to do anything in percentage. Simply, because percentages are pointless (e.g. it is pointless to have a JTextField 10% of the window size - since one can shrink the window so that JTextField becomes 0px wide, or can expand the window so that the JTextField is across two displays on a multi-display setup).
But, may times you may use the percentages to control sizes of bigger building blocks of your gui (panels, for example).
You can use JSplitPane where you can pre-set the ratio of the two sides. Or, you can use MigLayout which allows you to set such constraints in percentage, pixels, and other units.
我应该完全避免使用这些方法吗?
我不会说“避免”它们。我想说的是,如果您认为自己需要它们,那么您可能做错了什么。组件尺寸根据上下文确定。例如,文本组件大小由您指定的行数和列数以及您可能选择的字体决定。如果您设置了按钮和标签的大小,则按钮和标签的大小将是图形的大小,或者是显示您设置的文本所需的空间。每个组件都有一个自然大小,布局管理器将使用它们来布局所有内容,而无需您指定大小。主要的例外是 JScrollPane,它的大小与其包含的内容无关。对于这些,我有时会调用
setSize()
,并通过调用JFrame.pack()
让该大小确定初始窗口大小。通常,我会让窗口大小决定 JScrollPane 的大小。用户将确定窗口的大小。许多布局管理器无论如何都会忽略您设置的尺寸,因此它们通常起不到多大作用。这些方法的定义是有原因的。那么我应该什么时候使用它们呢?在什么情况下?出于什么目的?
我相信添加它们是为了向布局管理器提供提示。它们的编写可能是出于历史原因,因为布局管理器是新事物,人们并不完全信任它们。我认识一些开发人员,他们避免使用布局管理器并手动放置所有内容,只是因为他们不想费心去学习新的范例。这是一个糟糕的主意。
使用这些方法到底会产生哪些负面后果? (我只能考虑在不同屏幕分辨率的系统之间添加可移植性)。
它们效率低下,而且会产生糟糕的布局,对象会被挤压或拉伸到不自然的尺寸。而且布局会很脆弱。更改窗口大小有时会破坏布局并将内容放在错误的位置。
我认为没有任何 LayoutManager 能够完全满足所有所需的布局需求。我真的需要为布局上的每一个小变化实现一个新的 LayoutManager 吗? 您不应该“实现”一个新的 LayoutManager。您应该实例化现有的。我经常在一个窗口中使用多个布局管理器。每个 JPanel 都有自己的布局管理器。有些人对嵌套布局犹豫不决,因为它们很难维护。当我使用它们时,我会为每个人提供自己的创建方法,以便更容易地了解每个人的作用。但我从不“实现”布局管理器。我只是实例化它们。
如果4的答案是“是”,这是否会导致LayoutManager类激增而变得难以维护?
如果您正在实现新的布局管理器类以实现布局的细微变化,那么您就错误地使用了它们。如果您只是实现新的布局管理器,那么您可能做错了什么。我唯一一次扩展 LayoutManager 类,是向 JScrollPane 添加缩放滑块。
在我需要定义组件子组件之间的比例的情况下(例如,child1 应使用 10% 的空间,child2 40%,child3 50%),是否可以在不实现自定义 LayoutManager 的情况下实现这一目标?< /强>
JSplitPane 有一种方法来指定每个组件应获得的百分比。默认情况下,分隔线是可移动的,但如果需要,您可以将其关闭。我不太使用这个功能。我通常有一些组件占用固定的大小,其余的空间则由滚动窗格占用。滚动窗格的大小将随窗口大小而调整。如果您有两个并排的滚动窗格,则可以将它们放入 JSplitPane 中,并指定当用户展开和收缩窗口时为每个滚动窗格分配的新空间的百分比。
Should I completely avoid the use of those methods?
I wouldn't say "avoid" them. I'd say that if you think you need them, you're probably doing something wrong. Component sizes are determined in context. For example, Text component sizes are determined by the number of rows and columns you specify, combined with the font you may have chosen. Your button and label size will be the size of the graphic, if you set one, or the space needed to display the text you set. Each component has a natural size, and the layout managers will use those to lay everything out without you needing to specify sizes. The main exception is the JScrollPane, which has a size independent of whatever it contains. For those, I will sometimes call
setSize()
, and let that size determine the initial window size, by callingJFrame.pack()
. Usually, I will let the window size determine the JScrollPane size. The user will determine the size of the window. Many layout managers ignore the sizes you set anyway, so they often don't do much good.The methods have been defined for a reason. So when should I use them? In which context? For what purposes?
I believe they were added to provide hints to the layout managers. They may have been written for historical reasons, because layout managers were new, and people didn't fully trust them. I know a few developers who avoided layout managers and placed everything manually, just because they didn't want to bother with learning a new paradigm. It's a terrible idea.
What exactly are the negative consequences of using those methods? (I can only think adding portability between systems with different screen resolution).
They're ineffective, and they produce bad layouts, with objects getting squeezed or stretched to non-natural sizes. And the layouts will be brittle. Changes to the window size will sometimes break the layout and put things in the wrong places.
I don't think any LayoutManager can exactly satisfy all desired layout needs. Do I really need to implement a new LayoutManager for every little variation on my layout ? You shouldn't "implement" a new LayoutManager. You should instantiate existing ones. I often use several layout managers in a single window. Each JPanel will have its own layout manager. Some people balk at nested layouts, because they're hard to maintain. When I use them, I give each one its own creation method to make it easier to see what each one does. But I never "implement" a layout manager. I just instantiate them.
If the answer to 4 is "yes", won't this lead to a proliferation of LayoutManager classes which will become difficult to maintain?
If you're implementing new layout manager classes for slight variations in layout, you're using them wrong. If you're just implementing new layout managers, you're probably doing something wrong. The only time I've extended a LayoutManager class, it was to add a zoom slider to a JScrollPane.
In a situation where I need to define proportions between children of a Component (eg, child1 should use 10% of space, child2 40% ,child3 50%), is it possible to achieve that without implementing a custom LayoutManager?
The JSplitPane has a way of specifying the percentage each component should get. The divider is movable by default, but you can turn that off if you want. I don't use that feature much. I usually have some components that take up a set size, and the rest of the space is taken up by a scroll pane. The scroll pane size will adjust with the window size. If you have two scroll panes side by side, you can put them in a JSplitPane and specify the percentage of new space given to each one as the user expands and contracts the windows.