简体   繁体   English

灵活的布局管理器,将根据调整大小来更改布局

[英]A Flexible Layout manager that will change the layout according to resize

In my JPanel , I have 6 buttons laid out in a row (using FlowLayout as of now). 在我的JPanel ,我有6个按钮排成一排(目前使用FlowLayout )。 The default size of the panel is enough to accommodate these buttons in one row. 面板的默认大小足以将这些按钮排成一行。 But, when the frame is resized it gets stuck at the size that is the sum of the minimum sizes of each button. 但是,当调整frame大小时,它会卡在每个按钮的最小大小之和的大小上。

I need a layout manager that simply puts the buttons in a new row on re-sizing of the panel. 我需要一个布局管理器,只需在调整面板大小时将按钮放到新行中。 I'm pretty new to Java Swing so I apologize in advance if this is a trivial question. 我对Java Swing还是很陌生,所以如果这是一个琐碎的问题,我先向您道歉。

MigLayout is by far the best layout manager I've ever used. 到目前为止, MigLayout是我使用过的最好的布局管理器。 Things that used to require nested containers and lots of hard to understand code can be done in a single container with simple to understand (and maintain) string constraints. 过去需要嵌套容器和许多难以理解的代码的事情可以在单个容器中完成,并且具有易于理解(和维护)的字符串约束。

The flow layout is capable of your desired behavior (moving components into new row if they cannot fit). 流布局能够实现所需的行为(如果组件无法容纳,则将其移动到新行)。 Check out the swing tutorial (run FlowLayoutDemo). 查看摇摆教程 (运行FlowLayoutDemo)。 You'll have to show us your source code to find out, whether there is some other constrain which prevents it. 您必须向我们展示您的源代码,以查明是否存在其他阻止它的约束。

FlowLayout does actually paint components on a new row, but the problem is that the preferred size of the panel doesn't change so in many cases you can't see the components (unless you happen to add the panel to the CENTER of a BorderLayout). FlowLayout实际上确实在新行上绘制了组件,但是问题是面板的首选大小没有改变,因此在许多情况下您看不到组件(除非您碰巧将面板添加到BorderLayout的CENTER上) )。

One solution is to use the Wrap Layout , which extends FlowLayout to recalculate the preferred size of the panel so that you see the buttons on a new row. 一种解决方案是使用Wrap Layout ,它扩展了FlowLayout以重新计算面板的首选大小,以便您在新行上看到按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM