import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestBoxLayout extends JFrame { /* Le nombre d'éléments de nomsBoutons doit être égal à celui de alignements. */ public TestBoxLayout(String [][] nomsBoutons, float [] alignements) { super("BoxLayout"); Container c = getContentPane(); c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS)); int nColonnes = nomsBoutons.length; JPanel [] panels = new JPanel[nColonnes]; for (int i = 0; i < nColonnes; i++) { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); for (int j = 0; j < nomsBoutons[i].length; ++j) { JButton b = new JButton(nomsBoutons[i][j]); b.setAlignmentX(alignements[i]); panel.add(b); } c.add(panel); } addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String [] args) { String [][] nomsBoutons = {{"Heureux", "qui", "comme"}, {"Ulysse", "a", "fait", "un"}, {"beau", "voyage", "ou", "comme", "cestui-la"}, {"a", "conquis"}, {"la", "toison", "et", "puis"}}; float [] alignements = {Component.LEFT_ALIGNMENT, 0.25f, Component.CENTER_ALIGNMENT, 0.75f, Component.RIGHT_ALIGNMENT}; JFrame tbl = new TestBoxLayout(nomsBoutons, alignements); tbl.pack(); tbl.setVisible(true); } }