摆动动态(自动)贴合布局

考虑100(动态) JLabel对象,我想在可resize的JPanel显示它们。
目前我使用网格包布局(2列和50行),但是当用户resize并展开表单时,我希望(例如)4列25行,小表格(1列和100行)相同,以其他方式填满整个面板(没有任何空白区域)。
我知道这应该手动完成,目前我重绘(重绘)所有成员一旦用户调整表单,但我只是想知道是否有任何准备好的解决方案这样做。
提前致谢。

看看Rob Camick的WrapLayout

使用示例

在此处输入图像描述在此处输入图像描述

 import java.awt.*; import javax.swing.*; public class TestWrapLayout { public TestWrapLayout () { ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png")); JPanel panel = new JPanel(new WrapLayout()); for (int i = 1; i <= 250; i++) { JLabel iconlabel = new JLabel(icon); iconlabel.setLayout(new BorderLayout()); JLabel textlabel = new JLabel(String.valueOf(i)); textlabel.setHorizontalAlignment(JLabel.CENTER); textlabel.setForeground(Color.WHITE); textlabel.setFont(new Font("impact", Font.PLAIN,20)); iconlabel.add(textlabel); panel.add(iconlabel); } JFrame frame = new JFrame(); frame.add(new JScrollPane(panel)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { new TestWrapLayout(); } }); } } 

您可以在public void layoutContainer(Container target)方法中定义自定义LayoutManager以根据需要定位子项。

这里实现了类似的示例http://java-sl.com/tip_columns_flow_layout.html