1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test_gridbaglayout extends JFrame
{
public static GridBagLayout gbl_tpc = new GridBagLayout();
public static GridBagConstraints gbc_tpc = new GridBagConstraints();
public JButton a1 = new JButton("a1");
public JButton a2 = new JButton("a2");
public JButton a3 = new JButton("a3");
public JButton b1 = new JButton("b1");
public JButton b2 = new JButton("b2");
public test_gridbaglayout() throws HeadlessException {
super();
getContentPane().setLayout(gbl_tpc);
positionner(gbc_tpc, 0, 0, 2, 1, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
gbl_tpc.setConstraints(a1, gbc_tpc);
add(a1);
positionner(gbc_tpc, 2, 0, 2, 1, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
gbl_tpc.setConstraints(a2, gbc_tpc);
add(a2);
positionner(gbc_tpc, 4, 0, 2, 1, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
gbl_tpc.setConstraints(a3, gbc_tpc);
add(a3);
positionner(gbc_tpc, 0, 1, 3, 1, 50, 0, GridBagConstraints.NONE, GridBagConstraints.CENTER);
gbl_tpc.setConstraints(b1, gbc_tpc);
add(b1);
positionner(gbc_tpc, 3, 1, 3, 1, 50, 0, GridBagConstraints.NONE, GridBagConstraints.CENTER);
gbl_tpc.setConstraints(b2, gbc_tpc);
add(b2);
setVisible(true);
pack();
}
private void positionner(GridBagConstraints gbc,
int gx,
int gy,
int gw,
int gh,
int wx,
int wy,
int remplir,
int aligner) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.fill = remplir;
gbc.anchor = aligner;
}
public static void main(String[] args) {
new test_gridbaglayout();
}
} |