Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.plaf.metal.MetalLookAndFeel; public class Main { static final String LAF_METAL = MetalLookAndFeel.class.getName(); public static JPanel createFlow(final Object... components) { return createFlow(FlowLayout.LEFT, 0, components); } public static JPanel createFlow(final int align, final int gap, final Object... components) { final JPanel jp = new JPanel(new FlowLayout(align, gap, gap)); jp.setBorder(BorderFactory.createEmptyBorder()); for (final Object component : components) { if (component instanceof Component) { initComponentHeight((Component) component); jp.add((Component) component); } else if (component instanceof Number) { jp.add(Box.createHorizontalStrut(((Number) component).intValue())); } } return jp; } public static void initComponentHeight(final Component... components) { if (components == null) { return; } for (final Component component : components) { if ((component instanceof JComboBox) || (component instanceof JButton)) { component.setPreferredSize(new Dimension(component.getPreferredSize().width, 22)); } else if (component instanceof JTextField) { final String lf = UIManager.getLookAndFeel().getClass().getName(); int i = 22; if (lf.equals(LAF_METAL)) { i = 23; } component.setPreferredSize(new Dimension(component.getPreferredSize().width, i)); } } } }