Java 您推荐哪种 Swing 布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1832432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which Swing layout(s) do you recommend?
提问by Carl Smotricz
There are 8 layout managers in the Java library alone, and then there's a bunch of 3rd party products as well.
仅 Java 库中就有 8 个布局管理器,然后还有一堆 3rd 方产品。
The other day, I tried to use SpringLayout and... well, it ain't working out for me. See my other question. So... if you have a Swing app to design, and you want your layout just so, what are your preferred layout managers? Any interesting/useful experiences to relate?
前几天,我尝试使用 SpringLayout 并且......好吧,它对我不起作用。请参阅我的另一个问题。所以...如果你有一个Swing应用设计,并希望您的布局只是这样,什么是您的首选布局管理器?有什么有趣/有用的经历可以联系起来吗?
Update:Yup, sure I'm aware that different LMs are appropriate for different layouts. But I'd expect that most of you use 1, 2 or maybe 3 for most everything you do, or you swear by a single one that's so versatile you never need anything else. With enough responses, I'd expect to see something like a bell curve, with a peak on GridBagLayout or MigLayout (for example) and a long tail of a few stragglers using (say) BoxLayout or FlowLayout.
更新:是的,我当然知道不同的 LM 适用于不同的布局。但是我希望你们中的大多数人在所做的大多数事情中都使用 1、2 或 3,或者您发誓只有一个功能如此强大,您永远不需要其他任何东西。有了足够的响应,我希望看到类似钟形曲线的东西,在 GridBagLayout 或 MigLayout(例如)上有一个峰值,以及一些使用(比如)BoxLayout 或 FlowLayout 的落后者的长尾。
Hopefully, the distribution of responses will reveal some trends as to what people use most to Get Stuff Done™
希望响应的分布将揭示人们最常使用什么来完成工作™的一些趋势
UPDATE and Summary
更新和总结
OK, after almost 2 days, MiGLayout is definitely out in front! Its fans will be happy to hear that it looks like this layout will be entering the "official" library soon.
OK,差不多2天后,MiGLayout绝对领先!它的粉丝会很高兴听到这个布局看起来很快就会进入“官方”图书馆。
GroupLayout, FormsLayout and TableLayout are relatively new and haven't gotten much exposure. Perhaps others will be as surprised to find out about them as I was.
GroupLayout、FormsLayout 和 TableLayout 相对较新,并没有得到太多曝光。也许其他人会和我一样惊讶地发现他们。
采纳答案by Joonas Pulakka
MiGLayout, no doubt. Honestly, it's the only Swing layout manager I know of that makes any sense.
MiGLayout,毫无疑问。老实说,这是我所知道的唯一一个有意义的 Swing 布局管理器。
The mere fact that there are 8 layout managers in the core JDK is a good sign that the Swing creators had absolutely no idea about what they were trying to do. This is not to trash the rest of the Swing - it's a good GUI toolkit, except for the layout managers.
核心 JDK 中有 8 个布局管理器这一事实表明,Swing 创建者完全不知道他们要做什么。这并不是要破坏 Swing 的其余部分——它是一个很好的 GUI 工具包,除了布局管理器。
回答by Bart Kiers
There is no real answer to your question except that: it depends. It depends on what kind of frame (form) you are trying to create. I am no Swing-guru, but created a couple of (moderately advanced) GUI's and have never had the need to touch the GridBagLayout manager. I have always been able to create my GUI's using a combination of "easier" layout managers. For example, you can give your frame the BorderLayout and then place another layout in the SOUTH of that BorderLayout.
您的问题没有真正的答案,只是:这取决于。这取决于您尝试创建什么样的框架(形式)。我不是 Swing-guru,但创建了几个(中等高级的)GUI,并且从来不需要接触 GridBagLayout 管理器。我一直能够使用“更简单”的布局管理器的组合来创建我的 GUI。例如,您可以为您的框架指定 BorderLayout,然后在该 BorderLayout 的 SOUTH 中放置另一个布局。
回答by Hyman
It depends which kind of layout you need, that's why you have 8 of them:
这取决于您需要哪种布局,这就是为什么您有 8 个布局:
BorderLayout
, simple and quite useful to design normal content frames (with main content in the middle and helpers on sides).GridLayout
, useful when you have a grid of objects that should be of the same size (buttons? textfields?).GridBagLayout
, very flexible, needs some tweaking to be fine and it is quite verbose (but I raccomend it if you want to do things well).FlowLayout
, useless.. not a very layout: just placing item one next to another.CardLayout
, useful for tabs or subviews that must be switched.BoxLayout
, never used it too much.. it should be a sort of enhancedFlowLayout
but it's not enough flexible to be used intensively.
BorderLayout
,简单且非常有用的设计普通内容框架(主要内容在中间,助手在两侧)。GridLayout
,当您有一个大小应该相同的对象网格(按钮?文本字段?)时很有用。GridBagLayout
,非常灵活,需要一些调整才能正常工作,而且非常冗长(但如果你想把事情做好,我会推荐它)。FlowLayout
,没用......不是一个非常布局:只是将一个项目放在另一个旁边。CardLayout
,对于必须切换的选项卡或子视图很有用。BoxLayout
, 从来没有用过太多.. 应该是一种增强,FlowLayout
但它不够灵活,不能集中使用。
回答by glebm
Use IntelliJ IDEA with its GUI designer. Makes GridBagLayout easy.
将 IntelliJ IDEA 与其 GUI 设计器一起使用。使 GridBagLayout 变得简单。
回答by ninesided
GroupLayoutis pretty decent. It was originally intended for use by GUI Builder applications but I've found it to be very straight forward to code by hand too.
GroupLayout相当不错。它最初是供 GUI Builder 应用程序使用的,但我发现它也非常适合手动编写代码。
回答by Tom Hawtin - tackline
GridBagLayout
. Does pretty much all that you need (kind of) and it's in the Java library. Admittedly it does need some help, and the API is terrible.
GridBagLayout
. 几乎可以满足您的所有需求(有点),并且它在 Java 库中。不可否认,它确实需要一些帮助,而且 API 很糟糕。
GroupLayout
makes a real mess of your layout code. Okay, so most people's GUI code is a big ball of mud. But your's does not have to be! Perhaps a nice interface could be put onto this layout manager, but I suspect it might have to be cloned-and-owned.
GroupLayout
使您的布局代码变得一团糟。好的,所以大多数人的 GUI 代码是一团大泥球。但你的不一定是!也许一个不错的界面可以放在这个布局管理器上,但我怀疑它可能必须被克隆和拥有。
I am not in favour of introducing external dependencies unless they are really necessary. Also a lot of the third-party layout managers use strings of data, which have all the usual issues.
我不赞成引入外部依赖,除非它们真的有必要。此外,许多第三方布局管理器使用数据字符串,它们具有所有常见问题。
回答by clartaq
FormLayout
, part of the JGoodies Formspackage has been a workhorse for me. It's not perfectly flexible in that it works hard to make your design look good. I've used it for several years on dozens of projects and it keeps on producing good looking output quickly.
FormLayout
,JGoodies Forms包的一部分对我来说是一个主力。它并不完全灵活,因为它很难使您的设计看起来不错。我已经在数十个项目中使用它多年,并且它一直在快速产生美观的输出。
You specify your layout in human-readable text, then add the components. Done.
您可以在人类可读的文本中指定布局,然后添加组件。完毕。
回答by baybora.oren
I usually use border layoutwith gridlayout, first i design ui on paper prototype like ;
我通常使用带有gridlayout 的边框布局,首先我在纸原型上设计 ui ;
(source: usernomics.com)
(来源:usernomics.com)
After that we can split screen to gridlayout on borderlayout. In this picture we see NORTH, CENTER, SOUTH part (BorderLayout elements) and every part's layout can be gridlayout or BorderLayout, it depends on you. Same Layouts can use one within the other.
之后我们可以在borderlayout上将屏幕分屏到gridlayout。在这张图中,我们看到了 NORTH、CENTER、SOUTH 部分(BorderLayout 元素),每个部分的布局可以是 gridlayout 或 BorderLayout,这取决于您。相同的布局可以在另一个中使用一个。
回答by Michael Borgwardt
All of them, in combination.That's the whole point. Each layout manager fulfills different requirements, and by nesting panels with different layout managers, you can achieve almost anything.
所有这些,结合在一起。这就是重点。每个布局管理器都满足不同的要求,通过将面板与不同的布局管理器嵌套在一起,您几乎可以实现任何目标。
The "do everything in a single panel" layout managers like GridBagLayout
and GroupLayout
(and lots of 3rd party ones) have their place, mainly when you need components in different parts of the layout to align, but with a large layout, they generally result in a huge, hard-to-handle mess.
布局管理器像“在一个单一的面板做的一切”GridBagLayout
和GroupLayout
(和许多第三方的人的)有自己的位置,主要是当你需要在布局中对齐的不同部分组件,但有一个大的布局,它们通常导致巨大的,难以处理的混乱。
回答by rob
It depends on what kind of GUI you are creating. You might use just one or two of the simple layouts, or you might need to reach for a more advanced layout. My overall layout manager use would probably break down to something like this, although it would vary based on the project:
这取决于您要创建的 GUI 类型。您可能只使用一两个简单的布局,或者您可能需要使用更高级的布局。我的整体布局管理器使用可能会分解为这样的东西,尽管它会因项目而异:
- 65% GridBagLayout - The one layout that will get it done, no matter what you need to do.
- 15% Box/BoxLayout - Great for quickly & easily sticking a couple components together.
- 12% BorderLayout - Good for attaching a button panel or info panel to a content panel. I almost always use it to add content to a JFrame.
- 3% FlowLayout - Useful for button panels, but not much else.
- 3% CardLayout - Mostly useful in programs that display different content panels for different operational modes.
- 2% Other layouts - It's very rare that I need anything else, but occasionally one of the other layouts comes in handy.
- 65% GridBagLayout - 无论您需要做什么,都能完成的一种布局。
- 15% Box/BoxLayout - 非常适合快速轻松地将几个组件粘在一起。
- 12% BorderLayout - 适合将按钮面板或信息面板附加到内容面板。我几乎总是用它来向 JFrame 添加内容。
- 3% FlowLayout - 对按钮面板有用,但除此之外别无他用。
- 3% CardLayout - 主要用于针对不同操作模式显示不同内容面板的程序。
- 2% 其他布局 - 我很少需要其他任何东西,但偶尔其他布局之一会派上用场。
Once you get the hang of GridBagLayout, it's not that bad to write initially, but it's still not pretty to work with, or to debug later on. I tried MiGLayout for something recently and was disappointed to find that the MiGLayout actually ended up being morecomplicated to use than the GridBagLayout in that particular case.
一旦掌握了 GridBagLayout 的窍门,开始编写并没有那么糟糕,但是使用它或稍后进行调试仍然不是很好。我最近尝试了 MiGLayout,但很失望地发现 MiGLayout在这种特殊情况下实际上比 GridBagLayout 使用起来更复杂。
Some people try to avoid GridBagLayout like the plague; but the truth is, there are some things that no combination of simple layouts will be able to handle. It's fine to split a GUI into panels for different logical sections, but I think if you're creating a whole bunch of unnecessary extra nested panels just for the purpose of positioning components, you clearly need to learn how to use a GridBagLayout (or other similarly advanced layout, like MiGLayout). You might get your GUI to look okay with a nasty mess of nested BorderLayouts and GridLayouts and BoxLayouts, but as soon as someone starts resizing your application windows and dialogs to be either smaller or larger than you originally designed them, your GUI will probably look horrible and your customers will start to form a negative opinion about your product since you couldn't get such a simple thing right.
有些人试图避免 GridBagLayout 像瘟疫一样;但事实是,有些事情是简单布局的组合无法处理的。将 GUI 拆分为不同逻辑部分的面板很好,但我认为如果您创建一大堆不必要的额外嵌套面板只是为了定位组件,您显然需要学习如何使用 GridBagLayout(或其他类似的高级布局,如 MiGLayout)。你可能会让你的 GUI 看起来很好,嵌套的 BorderLayouts 和 GridLayouts 和 BoxLayouts 一团糟,但是一旦有人开始将你的应用程序窗口和对话框的大小调整为比你最初设计的更小或更大,你的 GUI 可能看起来很糟糕并且您的客户将开始对您的产品产生负面看法,因为您无法做到”
Update: I've been using WindowBuilder in Eclipse for a while now, and it greatly simplifies working with many layouts, especially GridBagLayout. I used to spend a lot of time writing layouts by hand, but with WindowBuilder or probably any similarly advanced visual editor, you can create the layouts in much less time.
更新:我在 Eclipse 中使用 WindowBuilder 已经有一段时间了,它极大地简化了许多布局的工作,尤其是 GridBagLayout。我曾经花费大量时间手工编写布局,但使用 WindowBuilder 或任何类似的高级可视化编辑器,您可以在更短的时间内创建布局。