MiGLayout, no doubt. Honestly, it's the only Swing layout manager I know of that makes any sense.
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.
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.
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:
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.
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 more complicated to use than the GridBagLayout in that particular case.
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.
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.
GroupLayout is 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.
FormLayout 是 JGoodies Forms 包的一部分,一直是我。它并不完全灵活,因为它很难使您的设计看起来不错。我已经在数十个项目中使用它多年,并且它不断快速地产生美观的输出。
您可以用人类可读的文本指定布局,然后添加组件。完毕。
FormLayout, part of the JGoodies Forms package 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.
You specify your layout in human-readable text, then add the components. Done.
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.
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.
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.
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.
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.
I only use GridBagLayout, especially when doing some complex interfacing, believe me GridBagLayout can do anything. I also recommended the FlowLayout, because it is easy to use, and understandable, good for putting a group of buttons. Actually I only use this two layouts since it is part of the JDK library as for the MigLayout I haven't tried it yet so for now can't recommend it for you.
I'm a Swing neophyte, but in the course of writing my first Swing application, I've tried four different layout managers: FlowLayout, BoxLayout, GridLayout and GroupLayout. In my opinion, FlowLayout and BoxLayout seems most appropriate for laying out groups of components of similar sizes. For components of varying sizes, GroupLayout seems to be the way to go. Steeper learning curve than the other two, but definitely worth it. As for GridLayout, I'd argue that you can achieve the same results you would with that layout manager by using a combination of FlowLayout and BoxLayout -- and you'd probably have more control of the placement of your components. But maybe that's just me :)
发布评论
评论(14)
MiGLayout,毫无疑问。老实说,这是我所知道的唯一一个有意义的 Swing 布局管理器。
核心 JDK 中有 8 个布局管理器这一事实本身就是一个好兆头,表明 Swing 的创建者完全不知道他们想要做什么。这并不是要废弃 Swing 的其余部分 - 除了布局管理器之外,它是一个很好的 GUI 工具包。
MiGLayout, no doubt. Honestly, it's the only Swing layout manager I know of that makes any sense.
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.
所有这些结合起来。这就是重点。每个布局管理器都满足不同的要求,并且通过使用不同的布局管理器嵌套面板,您几乎可以实现任何目标。
“在一个面板中完成所有操作”的布局管理器,如 GridBagLayout 和 GroupLayout(以及许多第三方布局管理器)有其用武之地,主要是当您需要在不同部分的组件时布局对齐,但对于大型布局,它们通常会导致巨大的、难以处理的混乱。
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
andGroupLayout
(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.这取决于您要创建哪种 GUI。您可能只使用一两个简单的布局,或者您可能需要使用更高级的布局。我的总体布局管理器使用可能会分解为类似这样的内容,尽管它会根据项目而有所不同:
一旦掌握了 GridBagLayout 的窍门,最初编写并没有那么糟糕,但使用或稍后调试仍然不太好。我最近尝试了 MiGLayout,但很失望地发现 MiGLayout 实际上比特定情况下的 GridBagLayout 使用起来更复杂。
有些人像躲避瘟疫一样试图避开 GridBagLayout;但事实是,有些事情是简单布局的组合无法处理的。将 GUI 分成不同逻辑部分的面板很好,但我认为如果您只是为了定位组件而创建一大堆不必要的额外嵌套面板,那么您显然需要学习如何使用 GridBagLayout(或其他类似的高级布局,如 MiGLayout)。你可能会让你的 GUI 看起来不错,因为有一堆令人讨厌的嵌套 BorderLayouts、GridLayouts 和 BoxLayouts,但是一旦有人开始调整你的应用程序窗口和对话框的大小,使其比你最初设计的更小或更大,你的 GUI 可能看起来会很糟糕你的客户将开始对你的产品产生负面看法,因为你无法把这么简单的事情做好。
更新:我在 Eclipse 中使用 WindowBuilder 一段时间了,它极大地简化了许多布局的使用,尤其是 GridBagLayout。我曾经花费大量时间手动编写布局,但使用 WindowBuilder 或可能任何类似的高级可视化编辑器,您可以用更少的时间创建布局。
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:
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 more complicated to use than the GridBagLayout in that particular case.
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.
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.
DesignGridLayout
看起来都很棒,并且通过流畅的界面易于使用嗯>。只需看一下示例:
只需几行干净的代码:
DesignGridLayout
both looks great and is easy to use through fluent interface.just look at example:
with just a few lines of clean code:
这取决于您需要哪种布局,这就是为什么您有 8 个布局:
BorderLayout
,对于设计普通内容框架(主要内容在中间,辅助内容在两侧)来说非常简单且非常有用。GridLayout
,当您有一个大小应相同的对象网格(按钮?文本字段?)时很有用。GridBagLayout
,非常灵活,需要一些调整才能正常,而且它相当冗长(但如果你想把事情做好,我会推荐它)。FlowLayout
,没用......不是一种非常布局:只是将一个项目放在另一个项目旁边。CardLayout
,对于必须切换的选项卡或子视图很有用。BoxLayout
,从来没有用过它太多。它应该是一种增强的FlowLayout
,但它不够灵活,无法集中使用。It depends which kind of layout you need, that's why you have 8 of them:
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.GroupLayout 相当不错。它最初是供 GUI Builder 应用程序使用的,但我发现它也可以非常直接地进行手动编码。
GroupLayout is 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.
FormLayout
是 JGoodies Forms 包的一部分,一直是我。它并不完全灵活,因为它很难使您的设计看起来不错。我已经在数十个项目中使用它多年,并且它不断快速地产生美观的输出。您可以用人类可读的文本指定布局,然后添加组件。完毕。
FormLayout
, part of the JGoodies Forms package 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.You specify your layout in human-readable text, then add the components. Done.
我通常使用 边框布局 和 gridlayout,首先我在纸质原型上设计ui,例如;
(来源:usernomics.com)
之后我们可以在 borderlayout 上将屏幕拆分为 gridlayout。在这张图中我们看到了NORTH、CENTER、SOUTH部分(BorderLayout元素),每个部分的布局可以是gridlayout或BorderLayout,这取决于你。相同的布局可以在另一个布局中使用一个布局。
I usually use border layout with gridlayout, first i design ui on paper prototype like ;
(source: 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.
GridBagLayout。几乎可以完成您需要的所有功能(某种程度上),并且它位于 Java 库中。不可否认,它确实需要一些帮助,而且 API 很糟糕。
GroupLayout
使布局代码变得非常混乱。好吧,大多数人的 GUI 代码都是一团泥。但你的不一定是这样!也许可以将一个不错的界面放到这个布局管理器上,但我怀疑它可能必须被克隆和拥有。我不赞成引入外部依赖,除非确实有必要。此外,许多第三方布局管理器使用数据字符串,这存在所有常见问题。
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.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.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.
使用 IntelliJ IDEA 及其 GUI 设计器。使 GridBagLayout 变得简单。
http://madbean.com/anim/totallygridbag/
Use IntelliJ IDEA with its GUI designer. Makes GridBagLayout easy.
http://madbean.com/anim/totallygridbag/
除了这个之外,你的问题没有真正的答案:这取决于情况。这取决于您想要创建哪种框架(形式)。我不是 Swing 专家,但创建了几个(中等高级)GUI,并且从未需要接触 GridBagLayout 管理器。我一直能够使用“更简单”的布局管理器组合来创建我的 GUI。例如,您可以为框架指定 BorderLayout,然后在该 BorderLayout 的南部放置另一个布局。
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.
我只使用 GridBagLayout,尤其是在做一些复杂的接口时,相信我 GridBagLayout 可以做任何事情。我还推荐了
FlowLayout
,因为它易于使用,并且易于理解,适合放置一组按钮。实际上我只使用这两种布局,因为它是 JDK 库的一部分,至于MigLayout
我还没有尝试过,所以现在不能向您推荐它。I only use
GridBagLayout
, especially when doing some complex interfacing, believe meGridBagLayout
can do anything. I also recommended theFlowLayout
, because it is easy to use, and understandable, good for putting a group of buttons. Actually I only use this two layouts since it is part of the JDK library as for theMigLayout
I haven't tried it yet so for now can't recommend it for you.有以下可用的布局选项:
在上面的这些布局中,MigLayout 是最推荐的一个,因为它是 swing 布局管理器。其他人还没有得到太多曝光。
There are following layout options available:
Out of these above MigLayout is the most recommended one as it is swing layout manager. There others have not got much exposure.
我是 Swing 新手,但在编写第一个 Swing 应用程序的过程中,我尝试了四种不同的布局管理器:FlowLayout、BoxLayout、GridLayout 和 GroupLayout。在我看来,FlowLayout 和 BoxLayout 似乎最适合布局相似大小的组件组。对于不同大小的组件,GroupLayout 似乎是最佳选择。学习曲线比其他两个陡峭,但绝对值得。至于 GridLayout,我认为您可以通过使用 FlowLayout 和 BoxLayout 的组合来实现与使用该布局管理器相同的结果 - 并且您可能可以更好地控制组件的放置。但也许这只是我:)
谢尔顿
I'm a Swing neophyte, but in the course of writing my first Swing application, I've tried four different layout managers: FlowLayout, BoxLayout, GridLayout and GroupLayout. In my opinion, FlowLayout and BoxLayout seems most appropriate for laying out groups of components of similar sizes. For components of varying sizes, GroupLayout seems to be the way to go. Steeper learning curve than the other two, but definitely worth it. As for GridLayout, I'd argue that you can achieve the same results you would with that layout manager by using a combination of FlowLayout and BoxLayout -- and you'd probably have more control of the placement of your components. But maybe that's just me :)
Sheldon