问题描述
有几次我因为建议使用以下方法而受到批评:
Several times I've been criticized for having suggested the use of the following methods:
- setPreferredSize
- setMinimumSize
- setMaximumSize
- setPreferredSize
- setMinimumSize
- setMaximumSize
在 Swing
组件上。当我想在显示的组件之间定义比例时,我没有看到任何替代它们的用法。我被告知这个:
on Swing
components. I don't see any alternative to their use when I want to define proportions between displayed components. I have been told this:
我在网上搜索了一下,但我没有找到任何关于这个主题的综合分析。所以我有以下问题:
I have searched the web a little bit, but I haven't found any comprehensive analysis of the subject. So I have the following questions:
- 我应该完全避免使用这些方法吗?
- 已经定义了这些方法的原因。那我什么时候应该使用它们?在哪种情况下?为了什么目的?
- 使用这些方法的负面后果究竟是什么? (我只能考虑在具有不同屏幕分辨率的系统之间添加可移植性)。
- 我认为任何LayoutManager都不能完全满足所有需要的布局需求。我是否真的需要为我的布局上的每个小变化实现一个新的LayoutManager?
- 如果对4的回答是是,这是否会导致难以维护的LayoutManager类的扩散?
- 在我需要在Component的子节点之间定义比例的情况下(例如,child1应该使用10%的空间,child2 40%,child3 50%),是否可以在不实现自定义LayoutManager的情况下实现这一点?
- 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?
推荐答案
是应用程序代码。
已经定义了方法的原因。那我什么时候应该使用它们?在哪种情况下?出于什么目的?
我不知道,我个人认为它是一个API设计事故。稍微强迫复合组件对孩子大小有特殊想法。 稍微,因为他们应该使用自定义LayoutManager实现他们的需求。
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.
一些(不完整,不幸的是链接因为将SwingLabs迁移到java.net)技术原因例如在或 @bendicott在他/她对。在社交方面,为你不幸的家伙提供大量的工作,他必须维护代码,并且必须追踪破损的布局。
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.
是的,有足够强大的LayoutManagers来满足非常接近所有布局需求。三巨头是JGoodies FormLayout,MigLayout,DesignGridLayout。所以不,在实践中,你很少编写LayoutManagers,除了简单的高度专业化的环境。
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.
(4的答案是不。)
在我需要定义组件的子项之间的比例的情况下(例如,子项1应使用10%的空间,子项2 40%,子项3 50%),是否可能在没有实现自定义LayoutManager的情况下实现这一目标?
任何Big-Three都可以,甚至不能GridBag(从来没有打扰过掌握太多力量太麻烦了。)
Any of the Big-Three can, can't even GridBag (never bothered to really master, too much trouble for too little power).
这篇关于我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!