问题描述
我曾多次因为建议使用以下方法而受到批评:
Several times I've been criticized for having suggested the use of the following methods:
- 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 类激增,从而变得难以维护?
- 在我需要定义组件子级之间比例的情况下(例如,child1 应该使用 10% 的空间,child2 40%,child3 50%),是否可以在不实现自定义 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.
例如 或link @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.所以不,实际上,除了简单的高度专业化的环境之外,您很少编写 LayoutManager.
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 题的答案是否".)
(The answer to 4 is "no".)
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 方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!